AWS Builder Center

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.

I am a student
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:
  1. Client Request: A user sends an HTTP request (e.g., POST /items) to Amazon API Gateway.
  2. API Routing: API Gateway acts as the "front door," receiving the request and routing it to the appropriate AWS Lambda function.
  3. Business Logic Execution: AWS Lambda executes our backend code (written in Python, Node.js, or Go) in a fully managed, stateless container.
  4. 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.
  1. Open the Amazon DynamoDB console.
  2. Create a new table named ItemsTable.
  3. Set the Partition key to id (String).
  4. 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.
  1. Navigate to the AWS Lambda console and click Create function.
  2. Choose Author from scratch.
  3. Function name: ManageItemsFunction
  4. Runtime: Node.js 20.x or Python 3.12 (choose your preference).
  5. Under Permissions, choose or create an execution role with permissions to read/write to your DynamoDB table (AmazonDynamoDBFullAccess or 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 client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
const TABLE_NAME = "ItemsTable";
exports.handler = async (event) => {
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.
  1. Go to the API Gateway console and click Create API.
  2. Choose HTTP API and click Build.
  3. Configure integrations: Select Lambda and choose your ManageItemsFunction.
  4. Define an API name (e.g., ItemsAPI) and configure routes (e.g., method: POST, resource path: /items).
  5. 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!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article