๐ 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
๐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
๐ฏInput the table name and partition key as below. Click create table
๐ข ## Creation of AWS Lambda Function
๐Go to AWS Management console, search for AWS Lambda. Click on create a function
๐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.
๐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
๐ข ## Creation of API Gateway using REST API
๐Go to AWS Management console, search for API Gateway. Go to REST API and click on build.
๐Click on OK if the below pop-up appears
๐Click on New-API, Give the API name and description as below and click Create API
๐Go to Actions, Click on Create Resource as below
๐Give the resource name and resource path, Click on create resource
๐Go to Actions, Click on create method as below
๐Give the method name as POST, integrate the lambda function (AddVehicle) which we already created in previous steps. Click on Save button
๐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
๐ Give the payload as below in Request body. Click on Test
{
"vehicle_id":1,
"type":"scooter",
"availability":"true"
}
๐ If you face the below error, then it is due to AccessDeniedException. We have to create required IAM role for Lambda function
๐ Go to AWS Management console, search for AWS Lambda. Go inside your function, Click on configuration and navigate to permissions tab
๐ Go back to API Gateway and re-run your tests.
๐ Navigate to Dynamo DB and verify the new entries been created
๐ In order to access the API outside like Postman tool, we need to deploy our API as below
๐ Give the stage name where we need to deploy and click on Deploy button
๐ซOpen the postman tool, copy the invoke URL and click on POST call as below
๐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/
Top comments (0)