Serverless application development with Node.js on AWS platform using Serverless framework

gkhn profile image Gökhan Olgun Originally published at itnext.io on ・4 min read

In this tutorial, we will create and deploy a serverless Node.js application on the AWS platform using various AWS services such as Lambda , Layers , DynamoDB , API Gateway using the Serverless framework.

source: http://serverless.com

Let’s install the Serverless framework first.

sudo npm install -g serverless

After installation is complete, you need to configure your AWS credentials. You can find the needed information below:

Serverless Framework - AWS Lambda Guide - Credentials

I’ll go with the aws configure option to keep things simple for now but I strongly recommend you to use IAM roles instead.

AWS SDK configuration

After completing the configuration you can test the Serverless framework with the following commands.

Create a new service using aws-nodejs template:

sls create --template aws-nodejs --path myService

Serverless CLI will create an empty hello world service for you with a serverless.yml and handler.js which you can test your serverless configuration.

What we’re going to do is creating a serverless application from scratch and deploying it to the AWS platform.

First, create serverless.yml file in the root of your project directory.

Let’s start configuring our serverless application.

In this part of serverless.yml file, we define our service and provider resources we’re going to use. The provider is AWS in our case.

Add the following lines to serverless.yml file.

We configured our service on dev stage and assigned the required permissions to access messages DynamoDB table; hence, we need to create messages table.

Now it’s time to create our Lambda functions.

Create a functions directory in the root of your project directory and put the following JavaScript files in it.

getMessages Lambda function will let us retrieve messages from our DynamoDB table.

putMessage Lambda function will let us put messages to our DynamoDB table.

Now, we’re ready to configure our Lambda functions in serverless.yml file.

We configured our function handlers and connected them to HTTP end-points using API Gateway.

You probably noticed that we put a reference to our Layers within our application.

From AWS documentation:

A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.

Important note about Layers:

Your function can access the content of the layer during execution in the /opt directory. Layers are applied in the order that's specified, merging any folders with the same name. If the same file appears in multiple layers, the version in the last applied layer is used.


Now it’s time to configure our Layers.

Create layers/reverser directory in the root of your project directory and put the following JavaScript file in it.

This is just a simple JavaScript module to reverse the input string that we’re going to use to test our Layers implementation.

Now, let’s add the following lines to serverless.yml file to configure Reverser Layer that we’ve just created.

Important note from AWS documentation:

A function can use up to 5 layers at a time. The total unzipped size of the function and all layers can’t exceed the unzipped deployment package size limit of 250 MB. For more information, see AWS Lambda Limits.


Finally, we’re ready to deploy our serverless application to the AWS platform by executing the following command in our terminal.

sls deploy

As a result of this execution, you should see a screen similar to the following.

To test your application you can use endpoints stated in the output of your sls deploy or you can invoke your Lambda functions with sls CLI in the root directory of your project.

Let’s try with sls CLI.

sls invoke -f putMessage --data '{"message":"test message"}'

And the result should be as following:

Now, let’s try to get the messages we put to messages table.

sls invoke -f getMessage

And the result should be as following:

Also, you can see your messages in your DynamoDB table on your AWS Console.

String reverse module worked well and the message in the table seems reversed.

You can find the project containing files used in this tutorial in the Github repository below.


Posted on by:


ITNEXT is a platform for software developers, engineers, IT architects, system engineers and IT enthousiasts to share knowledge, connect and connect.


Editor guide