
Courtesy:draw.io
Abstract:
Amazon API gateway created, maintains and secures API at a scale. Essentially it is collection of API’s that user leverages to make calls using methods such as GET, POST,etc. In this simple use case, we create a serverless API using API gateway, Lambda, and DynamoDB.
Setup:
Create Lambda IAM Role
We create an execution role that gives the function permission to access AWS resource.
IAM
Create Lambda Function
To create a Lambda function,
1. Search for Lambda in AWS console.
2. Click create function.
a. Select "Author from scratch". Use name LambdaFunctionOverHttps.
b. Select Python 3.9 as Runtime.
c. Under Permissions, select "Use an existing role”.
d. From the drop down Select lambda-apigateway-role that we created.
3. Replace the boilerplate coding with the following code snippet and click "Save"

Create a test event

Execute test.


At this point we are redy to create a DynamoDB table and API using our Lambda as backend.
Create DynamoDB Table

Table is created.


Create API
To create API ,
1. Go to API gateway in AWS console.
2. Click Create API.
3. Scroll down and select “Buil” for REST API.

4. Giving API name as DynamoDBOperations.

5. Click Create API.

Each API is collection of resources and methods that are integrated with backend HTTP endpoints, Lambda functions, or other AWS services. Typically, API resources are organized in a resource tree according to the application logic. At this time, you only have the root resource, but let's add a resource next.
Click Action and create resource.

Inputting DynamoDBManager in the resource name. The resource path get activated and create resource.

Create method
Create a POST Method for our API. With the "/dynamodbmanager" resource selected, Click "Actions" again and click "Create Method".


Our API-Lambda integration is done!
Deploy API
Click "Actions", select "Deploy API"

Running the solution.
To execute our API from local machine, we are going to use Postman and Curl command.
{
"operation": "create",
"tableName": "lambda-apigateway",
"payload": {
"Item": {
"id": "1234ABCD",
"number": 5
}
}
}
Listed the inserted item – to validate the item has been inserted in the DynamoDB, go to Dynamo console, select "lambda-apigateway" table, select "Items" tab, and the newly inserted item should be displayed.

List the inserted item – Postman

We have successfully created a serverless API using API Gateway, Lambda, and DynamoDB!
Comments