AWS Builder Center

Building a Production-Ready Serverless REST API with AWS Lambda, API Gateway, and DynamoDB

A step-by-step walkthrough for building, securing, and testing a scalable, zero-maintenance REST API on the AWS Free Tier.

Running standard virtual machines like EC2 for low-traffic student APIs can be inefficient and requires manual OS patching. Serverless architecture provides high availability, auto-scaling, and zero running cost when idle.
Here is how to deploy a CRUD backend using AWS Lambda, API Gateway, and DynamoDB.



Architecture Flow

  1. Client issues an HTTPS request.
  1. Amazon API Gateway validates endpoints and routes the payload.
  1. AWS Lambda executes business logic in an isolated container.
  1. Amazon DynamoDB stores and retrieves structured JSON data.



Step 1: Create the DynamoDB Table

  1. Open the Amazon DynamoDB Console and click Create table.
  1. Table name:  ItemsTable .
  1. Partition key:  id  (Type: String).
  1. Table class: DynamoDB Standard.
  1. Capacity mode: Select On-demand (to avoid provisioned capacity charges within free tier limits).
  1. Click Create table.



Step 2: Write the Lambda Handler

Create a Lambda function running Node.js 20.x:
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand, ScanCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
const TABLE_NAME = "ItemsTable";
export const handler = async (event) => {
const method = event.requestContext?.http?.method || event.httpMethod;
try {
if (method === "GET") {
const data = await docClient.send(new ScanCommand({ TableName: TABLE_NAME }));
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data.Items)
};
}
if (method === "POST") {
const body = JSON.parse(event.body || "{}");
await docClient.send(new PutCommand({
TableName: TABLE_NAME,
Item: {
id: body.id || Date.now().toString(),
name: body.name || "Default Item",
createdAt: new Date().toISOString()
}
}));
return {
statusCode: 201,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: "Item created successfully" })
};
}
return { statusCode: 405, body: "Method Not Allowed" };
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message })
};
}
};
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