This is a sample project to demonstrate how to build a REST API using AWS Lambda and API Gateway.
URL: Not deployed at the moment
- AWS CLI
- AWS Console Account
- Python 3.9
- AWS Lambda
- AWS API Gateway
- AWS IAM
- AWS CloudWatch
- AWS DynamoDB
Note: Assuming you have already created an AWS account and have the AWS CLI installed and configured.
Create an IAM policy with the following permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1428341300017",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "",
"Resource": "*",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow"
}
]
}
Create an IAM role for AWS Service - Lambda with the above policy attached.
Create a DynamoDB table with the following properties:
- Table name:
products
- Primary partition key:
id
(String) - Fields:
id
,name
,description
Create a Lambda function as described in the file LambdaFunctionOverHttps.py
in this repository.
The handler
works as the entry point for the Lambda function and takes the following parameters:
event
: The event data passed to the Lambda function which should contain the operation to be performed and the payload.context
: The runtime information of the Lambda function.
The following operations are supported:
get
: Get a product by IDcreate
: Create a new productgetAll
: Get all productsecho
: Return the event data [for testing purposes]
Here are the inputs for each operation:
get
:{"operation": "get", "payload": {"id": "1"}}
create
:{"operation": "create", "payload": "Item": {{"id": "1", "name": "Product 1", "description": "Description of Product 1"}}}
getAll
:{"operation": "getAll", "payload": {}}
To publish the Lambda function, run the following command:
zip function.zip LambdaFunctionOverHttps.py
aws lambda create-function --function-name LambdaFunctionOverHttps --zip-file fileb://function.zip --handler LambdaFunctionOverHttps.handler --runtime python3.9 --role <role-arn>
Create an API Gateway with the following properties:
- Resource name:
DynamoDBManager
- Resource path:
/DynamoDBManager
- Method:
POST
- API Key required:
true
Note: For API Key creation, need to attach a usage plan to the API Gateway.
Add CloudWatch logs to the Lambda function to log the events. The logs can be viewed in the CloudWatch console.
Test the API using POSTMAN at the following URL with POST method:
https://v8u02y61t5.execute-api.us-east-2.amazonaws.com/test/DynamoDBManager
Headers must contain:
{
'x-api-key': '<api-key>'
}
And the body must contain the operation and payload as described above.