Scalable Serverless API: Amazon API Gateway & AWS Lambda
Building a Serverless API with Amazon API Gateway, AWS Lambda, and mastering CRUD operations with DynamoDB: A Step-by-Step Guide

Devops Engineer
Search for a command to run...
Building a Serverless API with Amazon API Gateway, AWS Lambda, and mastering CRUD operations with DynamoDB: A Step-by-Step Guide

Devops Engineer
No comments yet. Be the first to comment.
Master the Deployment of a Django Blog Page with Auto Scaling, VPC, RDS, S3, CloudFront, Route 53, Certificate Manager, Lambda and DynamoDB on AWS

Enhance Your AWS Proficiency through Comprehensive Modules, Immersive Labs, and Real-World Application

Learn how to create a powerful serverless API using Amazon API Gateway, AWS Lambda, and DynamoDB. Follow step-by-step instructions to set up the project, create Lambda functions, and deploy the API. Interact with DynamoDB for CRUD operations.
In this blog article, we will walk you through the process of building a powerful serverless API using Amazon API Gateway, AWS Lambda, and DynamoDB. This step-by-step guide will help you understand the high-level design, set up the project, and deploy the API, making it clear and easy to follow.
Our goal is to create a serverless API that interacts with DynamoDB to perform CRUD operations (Create, Read, Update, Delete) and supports additional operations for testing. The API will be backed by an AWS Lambda function, which will handle incoming requests and interact with the DynamoDB table based on the payload.
To achieve our goal, we will have the following components:
Amazon API Gateway: This service will act as the frontend for our API, receiving HTTP requests and routing them to the appropriate Lambda function.
AWS Lambda: We will create a Python 3.7 Lambda function that serves as the backend for our API. It will handle various operations and communicate with the DynamoDB table.
DynamoDB: This NoSQL database service from AWS will store our data. We will create a table and define a primary key for efficient data retrieval.
For this project, we will be using the following AWS services and other tools:
Amazon API Gateway: To create and manage the API endpoints.
AWS Lambda: For serverless computing to process incoming API requests.
AWS DynamoDB: To store and retrieve data in a scalable and reliable manner.
AWS IAM: To allow the Lambda function to carry out the required task.
Postman: To test POST requests on the API and to inspect API responses.

Our API will have a single resource named "DynamoDBManager," which will handle various methods like POST, enabling different DynamoDB operations. Each API call will be routed to the appropriate Lambda function, allowing us to interact with the DynamoDB table seamlessly.
Open the roles page in the IAM console.
Choose Create role.
Create a role with the following properties.
Trusted entity: Lambda.
Role name: lambda-apigateway-role.
Permissions: The custom policy below has the permissions that the function needs to write data to DynamoDB and upload 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"
}
]
}


# Sample Python Code for the Lambda Function
from __future__ import print_function
import boto3
import json
print('Loading function')
def lambda_handler(event, context):
'''Provide an event that contains the following keys:
- operation: one of the operations in the operations dict below
- tableName: required for operations that interact with DynamoDB
- payload: a parameter to pass to the operation being performed
'''
operation = event['operation']
if 'tableName' in event:
dynamo = boto3.resource('dynamodb').Table(event['tableName'])
operations = {
'create': lambda x: dynamo.put_item(**x),
'read': lambda x: dynamo.get_item(**x),
'update': lambda x: dynamo.update_item(**x),
'delete': lambda x: dynamo.delete_item(**x),
'list': lambda x: dynamo.scan(**x),
'echo': lambda x: x,
'ping': lambda x: 'pong'
}
if operation in operations:
return operations[operation](event.get('payload'))
else:
raise ValueError('Unrecognized operation "{}"'.format(operation))

Before moving forward, let's test the newly created Lambda function. We'll do a sample echo operation, where the function should output whatever input we pass.
Click the dropdown menu of the "Test" button in the AWS Lambda Console.
Configure a test event with the following JSON payload:
{
"operation": "echo",
"payload": {
"somekey1": "somevalue1",
"somekey2": "somevalue2"
}
}

With the successful test, we can move on to the next steps!
Now, we need to create the DynamoDB table that our Lambda function will utilize.
Open the DynamoDB console.
Click "Create table."
Set the following settings for the table:
Table name: lambda-apigateway
Primary key: id (string)
Click "Create."

Our next step is to create the API using Amazon API Gateway.
Go to the API Gateway console.
Click "Create API."
Select "REST API" and give the API a name, such as "DynamoDBOperations."
Click "Create API."


Click "Actions" and then select "Create Resource."
Input "DynamoDBManager" as the Resource Name and click "Create Resource."




To make our API accessible, we need to deploy it to a stage.



Now that our API is deployed, let's test it by invoking the Lambda function and interacting with the DynamoDB table.
{
"operation": "create",
"tableName": "lambda-apigateway",
"payload": {
"Item": {
"id": "1234ABCD",
"number": 5
}
}
}
To execute the API from your local machine, you can use either Postman or cURL:

cURL: Use the following command in your terminal:
$ curl -X POST -d "{\"operation\":\"create\",\"tableName\":\"lambda-apigateway\",\"payload\":{\"Item\":{\"id\":\"1\",\"name\":\"Bob\"}}}" https://$API.execute-api.$REGION.amazonaws.com/prod/DynamoDBManager

{
"operation": "list",
"tableName": "lambda-apigateway",
"payload": {
}
}
You will see the two items that appended lambda-apigateway table as it is below screenshot.

And that's it! You have successfully created a serverless API using Amazon API Gateway, AWS Lambda, and DynamoDB!
Once you have completed the tutorial and want to clean up the resources:
Delete the DynamoDB table "lambda-apigateway" from the DynamoDB console.
Delete the Lambda function "LambdaFunctionOverHttps" from the AWS Lambda console.
Delete the API "DynamoDBOperations" from the Amazon API Gateway console.
Delete the "lambda-apigateway-role" from AWS IAM console.
Building a serverless API with Amazon API Gateway, AWS Lambda, and DynamoDB provides a scalable and cost-efficient solution for your applications. By following this step-by-step guide, you've learned how to create, deploy, and test your serverless API. Now you can leverage this knowledge to build various serverless applications with ease and confidence. Happy coding!
GitHub Repository: https://github.com/saha-rajdeep/serverless-lab
AWS Documentation:
https://repost.aws/knowledge-center/api-gateway-authentication-token-errors
https://repost.aws/knowledge-center/api-gateway-internal-server-error