Building Your First Serverless REST API with AWS Lambda, API Gateway, and DynamoDB
Learn how to design, build, and deploy a fully managed serverless REST API using AWS Lambda, API Gateway, and DynamoDB with best practices for scalability and cost.
As developers, we often want to focus on writing code and delivering features rather than managing servers, patching operating systems, or scaling infrastructure. Enter the serverless paradigm on AWS—a cloud-native approach that allows you to build and run applications without thinking about servers.
In this article, we’ll walk through how to architect and deploy a robust serverless REST API using three core AWS services: Amazon API Gateway, AWS Lambda, and Amazon DynamoDB.
Architecture Overview
Our serverless pipeline follows a clean request-response lifecycle:
- Client Request: A user sends an HTTP request (e.g.,
POST /items) to Amazon API Gateway. - API Routing: API Gateway acts as the "front door," receiving the request and routing it to the appropriate AWS Lambda function.
- Business Logic Execution: AWS Lambda executes our backend code (written in Python, Node.js, or Go) in a fully managed, stateless container.
- Data Persistence: The Lambda function reads from or writes to Amazon DynamoDB, a fully managed NoSQL database delivering single-digit millisecond performance at any scale.
Step 1: Setting up the DynamoDB Table
First, we need a data store to hold our items.
- Open the Amazon DynamoDB console.
- Create a new table named
ItemsTable. - Set the Partition key to
id(String). - Leave the settings on Default settings (On-demand capacity mode is ideal for unpredictable workloads or getting started) and click Create table.
Step 2: Creating the AWS Lambda Function
Next, let's write the compute logic to handle incoming data.
- Navigate to the AWS Lambda console and click Create function.
- Choose Author from scratch.
- Function name:
ManageItemsFunction - Runtime: Node.js 20.x or Python 3.12 (choose your preference).
- Under Permissions, choose or create an execution role with permissions to read/write to your DynamoDB table (
AmazonDynamoDBFullAccessor a custom scoped IAM policy).
Paste the following sample code (Node.js example) into your Lambda function:
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, PutCommand, GetCommand } = require("@aws-sdk/lib-dynamodb");
const { DynamoDBDocumentClient, PutCommand, GetCommand } = require("@aws-sdk/lib-dynamodb");
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
const TABLE_NAME = "ItemsTable";
const docClient = DynamoDBDocumentClient.from(client);
const TABLE_NAME = "ItemsTable";
exports.handler = async (event) => {
try {
const httpMethod = event.requestContext.http.method;
try {
const httpMethod = event.requestContext.http.method;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (httpMethod === "POST") {
const body = JSON.parse(event.body);
const command = new PutCommand({
TableName: TABLE_NAME,
Item: { id: body.id, name: body.name, description: body.description }
});
await docClient.send(command);
return { statusCode: 201, body: JSON.stringify({ message: "Item created successfully!" }) };
}
return { statusCode: 400, body: JSON.stringify({ message: "Unsupported method" }) };
} catch (error) {
console.error(error);
return { statusCode: 500, body: JSON.stringify({ error: error.message }) };
}};
Step 3: Configuring Amazon API Gateway
Now we expose our Lambda function to the web via an HTTP API.
- Go to the API Gateway console and click Create API.
- Choose HTTP API and click Build.
- Configure integrations: Select Lambda and choose your
ManageItemsFunction. - Define an API name (e.g.,
ItemsAPI) and configure routes (e.g., method:POST, resource path:/items). - Complete the wizard and copy your newly generated Invoke URL.
Testing Your API
You can test your newly deployed serverless architecture using
curl or Postman:Bash
1
2
3
curl -X POST https://<your-api-id>.execute-api.<region>.amazonaws.com/items \
-H "Content-Type: application/json" \
-d '{"id": "123", "name": "Serverless Guide", "description": "Intro to AWS Lambda"}'
If everything is configured correctly, you'll receive a
201 Created response, and you can verify the item directly inside your DynamoDB table items view.Conclusion
By combining API Gateway, Lambda, and DynamoDB, you've built a scalable, secure backend without provisioning a single EC2 instance or worrying about server maintenance. Serverless architectures scale automatically with your traffic and charge you only for the exact compute milliseconds you consume.
What serverless patterns are you building on AWS? Let me know in the comments below!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article