How to create REST API using Amazon API Gateway + AWS Lambda(Serverless) + Dynamo DB and test using a POSTMAN tool
Tags: aws, dynamodb, lambda, cloud
Published: 2023-11-10 03:34:07

> 💠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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrg3oit4mgw4pycc47nc.gif)

**📚Table of Content**
📢-[Creation of Dynamo DB table](#Creation of Dynamo DB table)
📢-[Creation of AWS Lambda Function](#Creation of AWS Lambda Function)
📢-[Creation of API Gateway using REST API](#Creation of API Gateway using REST API)
📢-[Test the REST API created using payload](#Test the REST API created using payload)
🌍-[Instructions to clean up AWS resource to avoid Billing](#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uatlgij2aft1rq2qhx7.png)

🎯Input the table name and partition key as below. Click create table

![table creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/660nhtpdpbdfsj08gxqg.png)

![Table status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atnpg7r6tue0qv4zwdp6.png)

📢 ##  **Creation of AWS Lambda Function**
📌Go to AWS Management console, search for AWS Lambda. Click on create a function

![AWS Lambda](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/231f0jy0ch24pjdr19tk.png)

📌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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vo6p923hz7izjkmf0kq.png)

![Function created successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49e1txu5dx6091ddbhea.png)

📌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"}
```
📌 Successfully deploy the code by clicking on Deploy button

![Deploy code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j02b19ved9ngpjtvra9k.png)

📢 ## **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](https://devway-to-uploads.s3.amazonaws.com/uploads/articles/zrjqr9jo2diy3vqm3c2v.png)

🎈Click on OK if the below pop-up appears

![pop-up window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80665erpptk2lvpjdgqh.png)

🎈Click on New-API, Give the API name and description as below and click Create API

![Amazon API Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdqhr1s1lj5jnp0n21d0.png)

🎈Go to Actions, Click on Create Resource as below

![create resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqu9i72ao9x2eoo4o3po.png)

🎈Give the resource name and resource path, Click on create resource

![create resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4bmpc7pjk33jdbjqh19.png)

🎈Go to Actions, Click on create method as below

![Create method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lg2psukmxda46u59wwrf.png)

🎈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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5drszttcvkvif5czzdij.png)

🌊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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psvz3z7kfhcio5jdtlrt.png)

📍 Give the payload as below in Request body. Click on Test

```
{
"vehicle_id":1,
"type":"scooter",
"availability":"true"
}
```
![Request payload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dss3wyp1ub3wunkqq13.png)
📍 If you face the below error, then it is due to AccessDeniedException. We have to create required IAM role for Lambda function

![Exception error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/laxta7rp6vk8d1jcfsy5.png)

📍 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsxjrkvboid8fdlz30o1.png)

📍 Go back to API Gateway and re-run your tests.

![validate response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs68cnd01drg05yutl9n.png)
📍 Navigate to Dynamo DB and verify the new entries been created

![Dynamo DB table with entries been created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo6gdl530hzi4o7n4zz7.png)

📍 In order to access the API outside like Postman tool, we need to deploy our API as below

![Deploy API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w3xmdyrb7e4u8o03fjj.png)
📍 Give the stage name where we need to deploy and click on Deploy button

![Deploy API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciha4glxxh0ihwacxoud.png)
💫Open the postman tool, copy the invoke URL and click on POST call as below

![POST CALL API Request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dk69jkwc0ztubh8e1m55.png)

**🌍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