Deploying your first AWS Lambda function using Serverless Framework

Omal Vindula
Oct 19 · 5 min read

We have talked about how to configure and setup the Serverless Framework to work with AWS Lambda through our previous article, which you can find here. We will pick up from where we left and will continue in this article. I highly recommend taking a few minutes to go through the previous article so that you’re up to speed! Now that we have set up everything, you should have a set of files in your project directory as follows.

Project Directory after setting up

Let's talk about the files that you have in the directory before proceeding.

“.gitignore” is the file used to exclude files when you’re committing your code to GitHub. For the time being lets just “ignore” it (pun intended). The other 2 files are quite important for what we are gonna do.

“handler.js” will contain the actual code that you wish to deploy into AWS Lambda. You will write your Lambda functions within this file. By default, it should have a small “hello world” like code as shown below for you to get started.

In this file, you can see that there’s a function called “hello” as a template for you to start with.

“serverless.yml” can be said as your configuration file which contains the settings in AWS Lambda such as the AWS region, the runtime you are using and many more. This file will also specify the API endpoints for your AWS Lambda functions. You will use these endpoints to call your Lambda functions once deployed. It should look similar to what is shown below.

Default serverless.yml file contents

In “serverless.yml”, you can see that I have specified my stage as “dev” and I want my lambda function to be deployed in “us-east-1” region. AWS has many regions to choose from, it is better to select a region that is close to you as it will improve the response times. Scroll down the file and we can also see the name of our default function.

This is where your functions should be included

Whenever you write a new function in the “handler.js” file, you will have to include the function in the .yml file under functions: as you would have to tell the .yml file that such a function exists. So, let's write a very basic function which takes 2 numbers as an input and returns their addition.

Be mindful about the usage of callback

Above function is very simple and pretty much self-explanatory. One thing to note is that the usage of a callback function to return the value as the usual return() will not work with AWS lambda because they are asynchronous functions. Also, make sure to include the getAddition() function in the serverless.yml file. Now the serverless.yml file should look like this.

We have included our first function in the serverless.yml, cool!

Now we just have to deploy our 2 functions, hello() and getAddition() using the command serverless deploy on your favourite terminal and voila! your lambda functions should be deployed on AWS very soon.

If all goes well, this is how your terminal should look like

Fun fact: Deploying lambda functions will also use an AWS S3 bucket. So, keep that in mind!

Now head-over to AWS console and go to AWS Lambda Management console and…


Rest assured, if you look closely on the top left of my page, it says “Singapore” as my current region and if you followed this article well, you should recall that I included “us-east-1” as my region in the serverless.yml file. We all know that Singapore isn’t in the US and this is a good time to think about the importance of deciding a region for the AWS Services that you use. Some services are global and the others (like AWS Lambda) have an AWS Region. If we switch the region to “us-east-1”, it should appear (hopefully!) on the console.

As expected, there they are!

We can see that our 2 lambda functions are now listed in the AWS Console and are ready to run! Now let's test out our new function and see whether it works as intended. Click on the name of the lambda function and it should take you to a screen as follows.


Here you can take full control of your lambda function and do things like how much resources (memory, timeout etc.) should AWS allocate? which AWS services (cloudwatch, S3 etc.) should be linked? If your function is not too large, you can edit your lambda function right in the editor provided in this page so that you don't have to re-deploy every time you do a change in the function.

There is also a test functionality which we are going to use and test whether our function works as intended. Click on the “Test” button on the top-left corner and it should open up a window to configure the test.

I included 2 parameters numberOne and numberTwo which we referred to in our lambda function (as event.numberOne and event.numberTwo) and assigned them values 420 and 69 (heh.). Save it and run the test by pressing the “Test” button once again. It should show a result as shown below.

420 + 69 = 489!

As you can see above, the response is 489 which is indeed the answer of 420 + 69. You can also see the other information like the duration and memory it took and what you were billed for. Now you have set-up, wrote and deployed a fully functional lambda function! Congratulations!

This is just the beginning and you can find so many examples/templates of different serverless scripts in the Serverless Examples Repository. Take a look here as they have so many cool stuff and they might even already have an answer for a function you’re looking for. So go take a look! See you on the other side!