A Complete Guide on Deploying AWS Lambda using Serverless Framework

A Complete Guide on Deploying AWS Lambda using Serverless Framework

Learn how to deploy your AWS Lambda functions with ease using the Serverless Framework in this comprehensive guide.

Although our goal is to build an entire project, I have made all my blogs independent of each other, you can still read this blog if you need complete information on depoying a AWS lambda function using Serverless Framework.

Alright, if you have been following my previous blogs on the serverless password manager, I assume you have learned about Encryption, Amazon DynamoDB, and data modeling with DynamoDB. Today we are going to set up our initial project using Serverless Framework!

You can check out all the previous blogs at the below links if you haven’t read them:

With that said, let’s get started!

What is this Buzz-word “Serverless” all about?

If you remember, when we were in college, we were taught all about Servers, Storage, DBs, and whatnot!? We all understood that in order to host an application, somewhere we need to maintain a Server, and not just that, as and when our application grows, the Servers also need to be upgraded and they have to be maintained, and also security is the biggest aspect! You need to have a separate team altogether to handle all these things right?

Well, Now! Nothing of that is required! AWS provides you an out-of-the-box Service called AWS Lambda, which lets you focus on the actual code, rather than having the Servers to be maintained, and that’s what is Serverless, having no Servers at all!

What is AWS Lambda?

The biggest confusion that a newbie gets is, how can we focus on just code and not the servers? how can we not have a server and make our application run? Well, the simple thing to understand here is, that rather than you hosting the entire application, you will now host the code!

Next confusion, Wait what? how do you host just the code?

Lambda stores your code in an internal S3 bucket that is private to your AWS account, and that’s how the code is hosted on the AWS console, and with the help of API Gateway you can associate a web API to a lambda, which routes the application request to the specified lambda function, and that’s how the building blocks of your application are created.

You can find more information on AWS Lambda here.

What is Serverless Framework?

Serverless Framework is a well-known framework to develop, deploy and maintain your serverless resources on AWS. Serverless framework provides you with IaC (Infrastructure as Code) which is a way of creating, deploying and maintaining your services or resources on the cloud with machine-readable configuration files.

Previously, the AWS CloudFormation was a highly used service to provision your AWS resources, the configurations specified in the raw CloudFormation files are quite lengthy and very detailed. However, Serverless Framework helps us to write small and concise configuration files and handles the rest of it behind the scenes and that’s how it behaves as a wrapper on top of the CloudFormation service.

Alright, let’s see how to leverage the Serverless Framework and deploy our first AWS Lambda function on the cloud.

Implementation

To get started, you will have to install NodeJs, because it’s a dependency for Serverless Framework. You can install it from this website. Please follow the steps given on the website because this blog is totally focused on AWS Lambda and Serverless Framework.

Once NodeJs is installed on your computer, you can check it by going to the command prompt and typing node Check out the below snap.

Once you type node and press enter, a node shell opens. This means the installation is successful.

The next step is to install the Serverless Framework. To install it type the below command on the terminal:

npm install -g serverless

The above command will install the Serverless Framework globally on your computer. To check if it is properly installed, you can just check the version in the command prompt.

serverless --version

This will give you the version details for the framework.

That’s it! we are now done, but hold on, make sure your AWS Credentials are configured on your computer, for which you need to install the AWS CLI on your computer, you can follow the instructions here. It has the instructions for Linux, Windows, and macOS. This is important not only for Serverless Framework but also for interacting with AWS Services. So go ahead! Do it if you haven’t configured it, it’s a one-time thing!

Now that we are all set, let’s get started on Deploying our Lambda function, it’s a quick and small process, follow the below steps!

Create a folder and open it in VSCode or Pycharm, you can use any of your favorite IDEs. Open your terminal, the Professional IDEs come with a built-in terminal.

Type serverless in your terminal and press enter, you will be shown a prompt as below.

These are the options for templates, here you can select anyone by using the up and down arrow keys on your keyboard, I have selected AWS-Python-Starter

Once you select it, you will be asked to give a name for your project. Specify any name of your choice and press enter. Since we are building a serverless password manager I have given the name is EzyPass.

Once it is done, a python template will be downloaded into your folder.

Next, you will be asked what org do you want to add this service? Now, this is with respect to your serverless dashboard, you should have a serverless account to use the dashboard, tahir1211 is my username, if you don’t have an account, you can choose to skip this part.

We are almost done! be careful in this next step, it asks Do you want to deploy now? If you have multiple AWS accounts configured in your computer, this option will take the default account and deploy your lambda wherever the default account’s credentials point to! I chose No because I want to deploy it later.

That’s it! our initial project setup is done. All we have to do now is test it locally and see if it works fine! The below files will be generated once we have run all the above commands.

Below is the minimal code that is provided in the template.

To test it out locally I would suggest you create a dictionary called event inside the function because we are going to simulate the request that comes to this lambda function.

event = {“greeting”: “Hello World”}

You can add this as the first line inside the function.

The serverless.yml is the most important file, it contains information regarding our AWS resources, and we have to specify the resource configurations in this file itself.

This is the serverless configuration provided by the template, most of the things are self-explanatory, the service is our project name, and it has described the serverless framework version as well. The main thing to focus on is the functions. Every lambda function that you create as a part of this project must be registered in this place, the hello is the function name, and handler is the path to the function. If you noticed, the filename is handler.py and the function name is hello .

That’s all! Now to test this, all you have to do is type the following command.

sls invoke local -f <func_name>

-f stands for function name that you have registered in the serverless.yml file, note that function names specified inside the .py files can be different and the ones that you register in the serverless.yml file can be different.

The command that we have used for testing has more optional parameters, let’s understand some important of them below.

sls invoke local -f <func_name> -p <input_file_path> --region <aws_region> --aws-profile <profile_name>

-p is for the input file path, since we already created a dictionary called as event in the function itself, we didn’t use this. However, if you want to pass that dictionary as a request, create a JSON file and create an object inside it, and provide the relative path of that file to this parameter.

--region This is the AWS region, if you don’t provide this, the default one will be picked.

--aws-profile As I said, there can be multiple accounts configured on your computer, you can use this parameter to specify in which account this function exists.

Alright, we have done everything, the only thing that is pending is the deployment of our lambda function.

All you need to do is type serverless deploy or sls deploy you can use --aws-profile along with this command to specify where to host the lambda function. Once you run this command, a CloudFormation stack will run, you can open your AWS console and see the progress by going to AWS CloudFormation as it is getting deployed! Make sure you are in the same region that you have specified on your computer.

You can use sls or serverless for commands, both resemble the same.

You can find out all the resources that got deployed by going to CloudFormation and selecting your stack. In my case it is EzyPass-dev. If you go to Lambda console, you can find the list of functions you create. Below you can see the function that we hosted.

And this is how your code looks in the console.

You can also test it by clicking on the Test button given on Lambda Console.

Alright with that said, This tutorial ends here! I hope you it was worth your time. For more information, check out these links to docs: Serverless and AWS Lambda.

Conclusion

First of all, thank you for spending your valuable time on this article, I really appreciate it. In the next blogs, I will talk about provisioning the DynamoDB on the cloud and the final implementation of our password manager.