Creating and deploying a Serverless REST API with AWS Lambda, DynamoDB and API Gateway and node.js

AWS Lambda is a compute service that lets you run code without provisioning or managing servers.

Amazon DynamoDB is a fast and flexible nonrelational database service for all applications that need consistent, single-digit millisecond latency at any scale.

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.

Let us see how we can build a RESTful API in Node.JS with the Serverless approach using AWS LambdaAPI Gateway & DynamoDB.

Step 1) Sign in to your AWS Management Console and Go to Services ->Compute -> Lambda

w1

Step 2) Create a Lambda function and enter details as below.

In the role, create a custom role with DynamoDB Full Access Policy in AWS IAM.

w2

w3.PNG

Step 3) Go to DynamoDB and Create a table.

w4.PNG

Click on Create table:

w5.PNG

Enter the table name and primary key as shown below:

w6

Click on create.

Your table has been created.

w7

Add other required columns like author name and book name.

w8w9

Step 4) Go back to your Lambda function. Add the following code for ‘POST’:

w17

Step 5) Add Table name in Lambda Environment Variables.

w11

Step 6) Configure Test Event to execute function with example data.

w12

Add test data and click on create:

w16

Step 7) Now Save and Run the function: Click on “Test”

Finally your lambda function is executed successfully,

w15

Go to your DynamoDB table and check the data:

w19.PNG

 

Step 8) Create another Lambda function  to fetch the ‘GET‘ DB details:

w20.PNG

Click on Create in above to create a new function.

Add the following code as shown below:

We will use scan method to query DynamoDB table

w23.PNG

w21

Add the Table Name as environment variable for this function also as done before.

w11

And Success!!

Click on TEST

You can easily GET details from DynamoDB Table

w22

So now our POST and GET are working!!!

Setting up the API Gateway

Go to API Gateway Console and create a new API

w24

Create a new API

w25.PNG

Enter the details as shown below: api name and description.

w26

Click on create.

Now Create a method: GET

Click on “Actions button drop down and select Create a method.

Enter the name of your Lambda Function in the details.

w27

Same as before, Create a method: POST

Enter the name of Lambda function.

w28.PNG

Deploy your API

w29.PNG

Choose Deploy API in the above menu.

Enter the Stage name as shown below and click on Deploy button

w30.PNG

Copy the invoke URL

w31.PNG

Test your API in the browser:

w32.PNG

Here it is your GET request is successfully working. For POST you can use a REST client like POSTMAN.

Congrats on successfully creating a Serverless REST API on cloud.


Post you comments below for any queries!

Advertisements

One thought on “Creating and deploying a Serverless REST API with AWS Lambda, DynamoDB and API Gateway and node.js

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s