Developing AWS Serverless RESTful API
Tags: aws, serverless, node, dynamodb
Published: 2020-01-27 06:51:02

This article was originally published at:  [https://www.ahmetkucukoglu.com/en/developing-aws-serverless-restful-api/](https://www.ahmetkucukoglu.com/en/developing-aws-serverless-restful-api/)

## 1. Introduction

This article series is about how to develop RestfulAPI with a serverless approach. We will use AWS as a cloud provider.

In this part, i will show the scenario and give information about what will be the result.

Our RESTful API endpoints will be like below.

[POST] api/ads  
[PUT] api/ads/{id}  
[DELETE] api/ads/{id}  
[GET] api/ads  
[GET] api/ads/{id}

We will use the following AWS services.

**API Gateway :** It will provide the endpoints we need for the RESTful API.  
**Lambda :** It will provide to write function for the GET, POST, PUT and DELETE requests.  
**DynamoDB :** It will provide NoSQL solution. In the POST, PUT and DELETE requests, we will write the data to the DynamoDB.  
**ElasticCache :** It will provide the distributed cache solution. In the GET request, we will read the data from Redis.  
**S3 :** It will provide our code to be versioned and stored after each deployment.  
**CloudFormation :** It will provide us to create and manage automatically all services mentioned above.

When the project is done, the architecture on AWS will be like below.

![Serverless](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/06-1024x607.png)

As a result, the record will be sent to the DynamoDB which is NoSQL solution of AWS in the POST request. Record changes in our DynamoDB table will be sended on the ElasticCache which is the solution service of the distributed cache of AWS. The data in the GET request will be read from ElasticCache instead of the DynamoDB.

## 2. Preparing the Development Environment

In this part, we will arrange our development environment.

Requirements

-   Visual Studio Code
-   AWS Account
-   AWS CLI
-   Serverless Application Framework

##### **2.1. Creating a User in AWS Console**

Creating an authorized user in AWS is required to make deployment with serverless.

Go to IAM from AWS Console. Choose "User" on the left menu. Click the "Add user" button.

Write "serverless" in the username fied. Choose "Programmatic Access" from the Access type section. Go to the second step(Permissions).

![Step 1 — User Informations](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/07-1024x550.png)

Choose "Attach existing policies directly" from the Permissions section. Give the following policies. Go to the third step(Tags).

-   AWSLambdaFullAccess
-   IAMFullAccess
-   AmazonS3FullAccess
-   CloudWatchLogsFullAccess
-   AmazonAPIGatewayAdministrator
-   AWSCloudFormationFullAccess
-   AmazonDynamoDBFullAccess
-   AmazonElastiCacheFullAccess

![Step 2 — User Permissions](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/08-1024x550.png)

Write "Application" in the Tag Key field and "Serverless RESTful API" in the Tag Value field. Go to the fourth step(Review).

![Step 3 — Tagging the User](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/09-1024x550.png)

Click the "Create User" button.

![Step 4 — User Review](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/10-1024x576.png)

On the screen, you will see the information about Access Key Id and Secret Access Key. Keep them in a side.

![Step 5 — Access Informations of the User](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/11-1024x550.png)

##### **2.2. The Installation of AWS CLI**

Open Command Prompt and run command line below.

```
aws configure --profile serverlessuser
```

**AWS Access Key ID :** The Access Key Id you will copy  
**AWS Secret Access Key :** The Secret Access Key you will copy  
**Default region name :** eu-central-1  
**Default output format :** json

##### **2.3. The Installation of the Serverless Framework**

We will use the Serverless Application Framework to develop this application. You can visit the address below for the installation.

[Serverless Framework - AWS Lambda Guide - Installing The Serverless Framework](https://s3-us-west-2.amazonaws.com/assets.site.serverless.com/logos/Serverless_mark_black_400x400_v3%402x.jpg)

## 3. Preparing the Endpoints

In this part, we will create the project and make deployment in a simplest way. We will not send a record to the DynamoDB yet. We will GET response after sending request to the endpoints.

Create the project folder with the command line below.

```
mkdir serverless-api && cd serverless-api
```
Create the serverless project with the command line below.

```
serverless create --template aws-nodejs --name ads-api
```

Open the project with the command line below via Visual Studio Code.

```
code .
```

Update the serverless.yml file like below.

serverless.yml v1

{% gist https://gist.github.com/ahmetkucukoglu/561d4a2bfd1aa0663ea386a1bdfc0603 %}

Delete the handler.js file. Create 4 files to be named as "create.js", "update.js", " delete.js" and "getByld.js".

create.js v1

{% gist https://gist.github.com/ahmetkucukoglu/27e628fc92c1cff535e15a848462930d %}

update.js v1

{% gist https://gist.github.com/ahmetkucukoglu/3c46a1c1d76efd14ea3b14c07e27cd3c %}

delete.js v1

{% gist https://gist.github.com/ahmetkucukoglu/31e20545b352818ca4ad8cc92ae41494 %}

getAll.js v1

{% gist https://gist.github.com/ahmetkucukoglu/2ecf6739346cba0d1267cd6047fb888d %}

getById.js v1

{% gist https://gist.github.com/ahmetkucukoglu/fd4e89d2f0405e4d2d2fb1016234f975 %}

Create a file named "create_request.json" in the project folder and paste the json in it. Defines the POST request model. Sets required fields.

create_request.json

{% gist https://gist.github.com/ahmetkucukoglu/2fe7f0c7827a6e45ee9f2a7984881970 %}

Create a file named "update_request.json" in the project folder and paste json in it. Defines the PUT request model. Sets required fields.

update_request.json

{% gist https://gist.github.com/ahmetkucukoglu/bc4a3e504aef5021493fdfc47ad2aaba %}

Make deployment to AWS by using the commad line below.

```
serverless deploy
```

After the deployment, in a simplest way, lambda functions and the API endpoints to make request functions is created.

After the deployment process, will write 5 API endpoints and 1 API Key on the console.

![Deployment output](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/12-1024x306.png)

> You can see the result by sending request to the endpoints. You need to add "x-api-key" to the Headers and for the value, write API Key showed in the console.

Let's look at what changed in AWS after the process.

Stack is created in the [Cloud Formation](https://eu-central-1.console.aws.amazon.com/cloudformation/home?region=eu-central-1).

![CloudFormation](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/13-1024x550.png)

Our code is sent to [S3](https://s3.console.aws.amazon.com/s3/home?region=eu-central-1).

![S3](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/14-1024x550.png)

[API Gateway](https://eu-central-1.console.aws.amazon.com/apigateway/home?region=eu-central-1) is created for the endpoints.

![API Gateway](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/15-1024x550.png)

[Lambda](https://eu-central-1.console.aws.amazon.com/lambda/home?region=eu-central-1#/functions) functions are created.

![Lambda](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/16-1024x549.png)

## 4. DynamoDB Integration

In this part, we will write the data coming over the API to DynamoDB. We will read the data from DynamoDB.

Install the required packages by using the command lines below.

```
npm i aws-sdk
npm i uuid
```

Update the serverless.yml file like below.

serverless.yml v2

{% gist https://gist.github.com/ahmetkucukoglu/918ed6445d3c1688b63aef6d59f0a131 %}

Update the files of create.js, update. js, delete.js, getAll.js and getByld.js like below.

create.js v2

{% gist https://gist.github.com/ahmetkucukoglu/dc8740e9938390dea49a374f2fc8f3de %}

update.js v2

{% gist https://gist.github.com/ahmetkucukoglu/abf8571f6f71805e5f94a40e5df03e91 %}

delete.js v2

{% gist https://gist.github.com/ahmetkucukoglu/677ea5553afb8e8ded91d7b97206143c %}

getAll.js v2

{% gist https://gist.github.com/ahmetkucukoglu/30eeddf93a61aca8fe661ad320026396 %}

getById.js v2

{% gist https://gist.github.com/ahmetkucukoglu/d05d47ba807ecf4a7d8a1a3085e873c8 %}

Make the second deployment to AWS with the command line below.

```
serverless deploy
```

> You can see the result by sending request to the endpoints. You need to add "x-api-key" to the Headers and for the value, write API Key showed in the console.

Let's look at what changed in AWS after the process.

A table is created in the [DynamoDB](https://eu-central-1.console.aws.amazon.com/dynamodb/home?region=eu-central-1#tables:).

![DynamoDB](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/17-1024x550.png)

## 5. Redis Integration

In this last chapter, we will transfer the streams in the DynamoDB(adding, udating and deleting) to ElastiCache Redis and do reading from Redis in the GetAll request.

Install the required packages with the command lines below.

```
npm i redis
```

Before starting to make update in the files, you need to learn the subnetlds by entering AWS console.

![Subnets](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/18-1024x576.png)

Update the serverless.yml file like below. You need to update the subnetlds parts with your own information.

serverless.yml v3

{% gist https://gist.github.com/ahmetkucukoglu/1992360572f8b847df7cd300685d59ce %}

Update the getAll.js file like below.

getAll.js v3

{% gist https://gist.github.com/ahmetkucukoglu/eff6969d3beeb74ffed09596e3d8a406 %}

Create a new file named trigger.js.

trigger.js

{% gist https://gist.github.com/ahmetkucukoglu/a5cfb6232ed416bcf87c39ee8903f05a %}

Make the third and last deployment to AWS with the command line below.

```
serverless deploy
```

> You can see the result by sending request to the endpoints. You need to add "x-api-key" to the Headers and for the value, write API Key showed in the console.

Let's look at what changed in AWS after the process.

[ElasticCache](https://eu-central-1.console.aws.amazon.com/elasticache/home?region=eu-central-1#redis:) is created.

![ElasticCache Redis](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/19-1024x576.png)

Trigger is assigned to the [DynamoDB](https://eu-central-1.console.aws.amazon.com/dynamodb/home?region=eu-central-1#tables:selected=AdsTable;tab=triggers).

![DynamoDB Trigger](http://www.ahmetkucukoglu.com/wp-content/uploads/2019/10/20-1024x550.png)

You can access the final version of the project from [Github](https://github.com/ahmetkucukoglu/serverless-restful).

{% github ahmetkucukoglu/serverless-restful %}

Good luck.