• About Us
  • Contact Us

Create Rest API with AWS API Gateway, Lambda and Dynamodb using Nodejs

Introduction

In this article we will focus in special AWS services who want to fast build and deploy endpoints, to GET/POST/PUT/DELETE information on a database regarding data of some application. The importance of server-less application are getting higher demand each day and aws available services offers a cheap and secured environment, so that the reason so many huge companies are migrating great part of their infrastructure to the cloud. We are using three awesome services API Gateway, Lambda and DynamoDB.

What is AWS API Gateway ?


Amazon API Gateway is a service provide by AWS that is used to create, publish, maintain, monitor & secure various API such as Rest APIs , HTTP & web socket at any scale. As an API developer you can create APIs that can access either AWS or any other web services along with the data store in the AWS cloud.

Lets start with Demo
Aim: To create an API with CRUD operation. Services Used -:

  • DynamoDB
  • Lambda
  • API gateway
  • IAM Service

Creating the DynamoDB Table
We will be using AWS DynamoDB a NoSQL database service to store data. DynamoDB is a fully managed, key-value, and document database that delivers single-digit-millisecond performance at any scale. Login to your AWS console and search for DynamoDB service.

  • Click on create table and enter table name, primary key always unique.
  • If you want to retrieve the data from dynamo db we should write the query to get the data by passing the key parameter.
  • If you want to retrieve the data you can retrieve it by using primary id.
  • Table name : member_table
  • Partition key : MemberId (String)

Create a Role & Policy
Before we go for Lambda to be able to access DynamoDB we need to give Lambda permission. Let’s create a new Policy.

  • Open AWS Console, type IAM in the search bar, and hit enter.
  • you can create role.
  • After clicking create role select AWS service and choose the service as lambda and click on next.
  • In next page attach policy search for AMAzonDynamoDBFullAccess, api_excute, cloudfullacess, select that policy and click on next.
  • Enter role name and click create role.
  • Now attache this role to lambda function under the execution role select IAM role which you have created.

Create a Lambda Function
AWS Lambda is event-driven computing rather than an imperative model. Events-driven computing responds to events when the event source happens, it triggers the lambda function. The event source could be a request to an endpoint which we will go through later using API Gateway.

  • Double click on the lambda service and click on create function
  • There you select an author from scratch, give some function name and select the language as Nodejs
  • After selecting the required parameters click on create function. then you will be getting a code editor. there you write your nodejs code for get/put data from dynamo DB.
  • Function name: users_details_api
  • Runtime: Node.js 14.x
  • Execution role: Use an existing role
  • Role name: myServerlessRole

The handler method is the method that will be executed when the lambda function is invoked. This function takes in two objects, event and context.
The next code is already tested and it’s working

Click on the Deploy button to deploy the Lambda function.

Create an API Gateway