Configuring Serverless Framework to work with AWS Lambda

Web development has been through a major revolution recently and it continues to evolve day by day. Most of it has happened due to the increasing number of frameworks in web development (not to be confused with Serverless Framework) as well as the emergence of cloud service providers such as AWS (Amazon Web Services), Google and Microsoft. Among all the services they offer, I find AWS Lambda fascinating. Gone are the days which we had to spend days provisioning and configuring our servers just to run a small piece of code on our website. AWS Lambda allows you to run a code (or a function) in the cloud without having to worry about configuring or maintaining a server. Just write your code and deploy in the cloud, you can start executing it from the next second.

AWS Lambda has many advantages, it is more cost-effective than having a server. Because AWS charges you only when your Lambda function is executing, as opposed to a traditional server which will run continuously whether your code is running or not. Therefore, you will not be charged a single cent when your Lambda function is not running.

Another advantage is that since everything is managed for you so that the developer will only have to focus on the core functionality of the code. Everything else such as configuring the environment and automatic scaling is handled by AWS. Of course, this means that the developer is losing some amount of control over the execution environment of their code. But that’s the sacrifice you’ll have to make for convenience that you get from AWS Lambda. So, make sure to weigh out the factors and decide whether you really need AWS Lambda as it comes with its limitations… But that’s a topic for another day.

So, how does the Serverless Framework relates to AWS Lambda? For start, Serverless Framework was first developed as a tool for building applications with AWS Lambda. But now, it has evolved and supports other FaaS (Function as a Service) providers such as Google Cloud Functions and Azure Functions through many different languages.

To start with Serverless, you’ll have to install the Serverless Framework CLI (Command Line Interface) on your computer. Installing that is simply a matter of typing a command on your terminal. But before doing that, you’ll have to install Node.js (Node version 6 or higher) as we are using the Node Package Manager (NPM) to install Serverless CLI on your PC.

Once you have installed Node.js, open up a terminal on your PC and type,

Once Serverless CLI is installed, go to your project root directory and open up a terminal there and type “serverless”. This will start a wizard in the terminal for serverless configuration which will guide you through the process of setting up serverless in your project.

As the first step, it will ask whether you want to create a new serverless project. Since it is our first time, we will say “yes”.

The next step is to specify your requirement, I’ll be integrating Serverless with a Node.js project to use with AWS Lambda. So, I will use the “AWS Node.js” option. But feel free to choose whichever option that suits you.

Then give a suitable name for your project when prompted. I will name it as “NewServerlessProject”.

Once these steps are followed, your terminal should look something like this

Now the terminal will prompt you to set your AWS credentials as you’ll be using a service provided by AWS. So, if you don’t have an AWS account, its time you go and make one now. AWS has a pretty generous free tier for starters, so you should not incur any cost as long as you hover below that free limit. AWS will inform you if a particular action/service will cost you. So, keep an eye out for that and you should be fine.

Once you have created an AWS account, say “yes” to both “Do you want to set them up now?” and “Do you have an AWS account?” on the terminal. Afterwards, it will prompt you to add AWS user access keys and will open up a browser window in AWS console to create a user in AWS IAM (Identity Access Management). Fill in the necessary details (Most of them must be already filled out for you) and create an IAM user for AWS Lambda.

If a browser window didn’t open in any case, head to IAM and go to the Users tab. AWS requires their services to act like “users” which we can attach permissions which specifies what that service can and cannot do. Similarly, we need to create a user for the AWS Lambda with necessary permissions for it to operate. I simply created a user with “administrator access”. You probably won’t need that much access but for the sake of simplicity, let us go with that. Make sure to add only required permission when you’re implementing in your projects.

Once you create a user, generate an access key for that user and make sure to download the security credentials of that user as it will not be available again to download once created. Keep the security credentials safe without sharing to any person to ensure that no one misuses your credentials. Open the downloaded file and you’ll see something called Access key ID and Secret Access Key, enter these credentials on the terminal when asked to.

Final steps in configuring Serverless Framework

And that’s it! If everything has gone well, you should see the following files in the “NewServelessProject” (or whichever the name you have given) folder.

The initial file structure after initiating Serverless with AWS Node.js

You have now successfully configured the Serverless Framework with AWS Lambda. Now it’s just a matter of typing your Lambda Function and deploying it into the cloud. In the next article, we will see how to implement and deploy a lambda function using Serverless!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store