DEV Community

Cover image for How to create REST API using Amazon API Gateway + AWS Lambda(Serverless) + Dynamo DB and test using a POSTMAN tool

How to create REST API using Amazon API Gateway + AWS Lambda(Serverless) + Dynamo DB and test using a POSTMAN tool

๐Ÿ’ In this use case, we will see how to build REST API using AWS Lambda, API Gateway and store it in Dynamo DB, user test the same using POSTMAN tool

## Flow Diagram

Flow Diagram

๐Ÿ“šTable of Content
๐Ÿ“ข-Creation of Dynamo DB table
๐Ÿ“ข-Creation of AWS Lambda Function
๐Ÿ“ข-Creation of API Gateway using REST API
๐Ÿ“ข-Test the REST API created using payload
๐ŸŒ-Instructions to clean up AWS resource to avoid Billing

๐Ÿ“ข ## Creation of Dynamo DB table
๐ŸŽฏ Go to AWS Management console, search for Dynamo DB. Click on create table as below

DynamoDB

๐ŸŽฏInput the table name and partition key as below. Click create table

table creation

Table status

๐Ÿ“ข ## Creation of AWS Lambda Function
๐Ÿ“ŒGo to AWS Management console, search for AWS Lambda. Click on create a function

AWS Lambda

๐Ÿ“ŒGive the function name as AddVehicle and runtime as Python3.10 and execution role as Create a new role with basic Lambda permissions. Click on Create function.

Create Function

Function created successfully

๐Ÿ“ŒGo to Code source, add the below python code in the code editor.

import boto3
dynamodb=boto3.resource('dynamodb')
table=dynamodb.Table('vehicles')

def lambda_handler(event, context):
    table.put_item(Item=event)
    return { "statusCode": 200,"message": "vehicle is created successfully"}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Successfully deploy the code by clicking on Deploy button

Deploy code

๐Ÿ“ข ## Creation of API Gateway using REST API
๐ŸŽˆGo to AWS Management console, search for API Gateway. Go to REST API and click on build.

API Gateway

๐ŸŽˆClick on OK if the below pop-up appears

pop-up window

๐ŸŽˆClick on New-API, Give the API name and description as below and click Create API

Amazon API Gateway

๐ŸŽˆGo to Actions, Click on Create Resource as below

create resource

๐ŸŽˆGive the resource name and resource path, Click on create resource

create resource

๐ŸŽˆGo to Actions, Click on create method as below

Create method

๐ŸŽˆGive the method name as POST, integrate the lambda function (AddVehicle) which we already created in previous steps. Click on Save button

Lambda function integration

๐ŸŒŠNote*: Click on OK if below pop-up appears for API Gateway permission to invoke the lambda function

๐Ÿ“ข ## Test the REST API created using payload

๐Ÿ“ Itโ€™s time to test the new API which has been created. Click on test in below screen which in turn will invoke your lambda function which will store the data created in Dynamo DB

Test the API created

๐Ÿ“ Give the payload as below in Request body. Click on Test

{
"vehicle_id":1,
"type":"scooter",
"availability":"true"
}
Enter fullscreen mode Exit fullscreen mode

Request payload
๐Ÿ“ If you face the below error, then it is due to AccessDeniedException. We have to create required IAM role for Lambda function

Exception error

๐Ÿ“ Go to AWS Management console, search for AWS Lambda. Go inside your function, Click on configuration and navigate to permissions tab

creation of new lambda role

๐Ÿ“ Go back to API Gateway and re-run your tests.

validate response
๐Ÿ“ Navigate to Dynamo DB and verify the new entries been created

Dynamo DB table with entries been created

๐Ÿ“ In order to access the API outside like Postman tool, we need to deploy our API as below

Deploy API
๐Ÿ“ Give the stage name where we need to deploy and click on Deploy button

Deploy API
๐Ÿ’ซOpen the postman tool, copy the invoke URL and click on POST call as below

POST CALL API Request

๐ŸŒInstructions to clean up AWS resource to avoid Billing

๐Ÿ“ŒDelete the table created which is vehicle in our use case example. We need to delete the items created before deletion of table.

๐Ÿ“ŒDelete the lambda function which has been created. In our use case it is AddVehicle.

๐Ÿ“ŒDelete the API which has been created in API Gateway along with resource.

Thanks for being patient and followed me. Keep supporting ๐Ÿ™

For more exercises โ€” pls do follow me below โœ…!

https://www.linkedin.com/in/vjraghavanv/

aws #awscommunitybuilder #awsreskill #awslambda #RESTAPI #postman #API-Gateway #DynamoDB

Top comments (0)

close