Serverless application development with Node.js on AWS platform using Serverless framework
Tags: aws, serverless, node, lambda
Published: 2019-12-13 15:14:12

---
title: Serverless application development with Node.js on AWS platform using Serverless framework
published: true
date: 2019-12-13 13:45:46 UTC
tags: aws,serverless,nodejs,lambda
canonical_url: 
---

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**](https://serverless.com/).

![](https://cdn-images-1.medium.com/max/1024/1*FE3KE3VOqec44eXbmWFBzw.png)<figcaption>source: <a href="https://serverless.com/">http://serverless.com</a></figcaption>

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](https://serverless.com/framework/docs/providers/aws/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.

![](https://cdn-images-1.medium.com/max/534/1*gI3Zqq2bQnT4lP7Wwum9Ww.png)<figcaption>AWS SDK configuration</figcaption>

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](https://serverless.com/framework/docs/providers/aws/examples/hello-world/node/) 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.

{% gist https://gist.github.com/gkhn/28cdb6013c997e474231e7a83ce9789c %}

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

{% gist https://gist.github.com/gkhn/83761fc01e6ec2a3b62940640bcb6ed7 %}

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.

{% gist https://gist.github.com/gkhn/3d3287ed2f48e1b89c07cd0d0b01fcbe %}

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

{% gist https://gist.github.com/gkhn/848e6fbf0e5fb36eba6b63e4b79a89fc %}

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.

{% gist https://gist.github.com/gkhn/93fd2af7b690b627c5521591bb295133 %}

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](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), 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.

> [https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)

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.

{% gist https://gist.github.com/gkhn/a70136e0c131a1b9e653b3fe345cca9f %}

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.

{% gist https://gist.github.com/gkhn/4d7094e999d7fc21aa215606e39830af %}

> **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](https://docs.aws.amazon.com/lambda/latest/dg/limits.html).

> [https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)

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.

![](https://cdn-images-1.medium.com/max/876/1*zfGfcHO9_Kjr5Ki2FV3GxA.png)

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:

![](https://cdn-images-1.medium.com/max/729/1*fW3K1-TaA-IIxLUzyzbOGg.png)

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

```
sls invoke -f getMessage
```

And the result should be as following:

![](https://cdn-images-1.medium.com/max/667/1*GZiIG3tu6DrpUVVV_Bm12g.png)

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

![](https://cdn-images-1.medium.com/max/1024/1*GuM7K-6YmHdc8YWa2umlsA.png)

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](https://github.com/gkhn/ServerlessAppWithLayers) below.

[gkhn/ServerlessAppWithLayers](https://github.com/gkhn/ServerlessAppWithLayers)

* * *