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)