This Document guides you through the process of setting up an Amazon API Gateway, a Lambda function, and a DynamoDB table. The API supports various DynamoDB operations and includes testing methods. The Lambda function is integrated with the API Gateway, allowing you to perform CRUD operations on a DynamoDB table.
- Open the roles page in the IAM console.
- Choose Create role.
- Create a role named
lambda-apigateway-role
with permissions for DynamoDB and CloudWatch Logs.{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1428341300017", "Action": [ "dynamodb:DeleteItem", "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Query", "dynamodb:Scan", "dynamodb:UpdateItem" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "", "Resource": "*", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Effect": "Allow" } ] }
- Click "Create function" in AWS Lambda Console.
- Select "Author from scratch," name it
LambdaFunctionOverHttps
, choose Python 3.7 as Runtime. - Under Permissions, select "Use an existing role," and choose the
lambda-apigateway-role
from the drop-down. - Click "Create function."
- Replace the code with the provided Python code snippet and click "Save."
- Configure a test event with the following JSON:
{ "operation": "echo", "payload": { "somekey1": "somevalue1", "somekey2": "somevalue2" } }
- Click "Test" to execute the test event.
- Open the DynamoDB console.
- Choose "Create table."
- Create a table named
lambda-apigateway
with the primary key asid
(string). - Click "Create."
- Go to the API Gateway console.
- Click "Create API" and select "Build" for REST API.
- Name the API as
DynamoDBOperations
and click "Create API." - Click "Actions," then click "Create Resource."
- Input "DynamoDBManager" as the Resource Name and click "Create Resource."
- Create a POST Method for the API with the "/dynamodbmanager" resource selected.
- Click "Actions" and select "Deploy API."
- Choose "[New Stage]" for "Deployment stage" and name the stage as "Prod."
- Click "Deploy."
- Obtain the API endpoint URL from the "Stages" screen.
- Use Postman to execute the API using a sample JSON payload for the create operation.
- Execute the provided JSON for DynamoDB create operation through Postman using the generated API Invoke URL.
{ "operation": "create", "tableName": "lambda-apigateway", "payload": { "Item": { "id": "1234ABCD", "number": 5 } } }
- Verify the item in DynamoDB console.
- Delete the DynamoDB table from the DynamoDB console.
- Delete the Lambda function from the Lambda console.
- Delete the API from the API Gateway console.
🎉 You have successfully created a serverless API with AWS Lambda, API Gateway, and DynamoDB! 🚀