Building a Serverless REST API on AWS - Part 1: Architecture, DynamoDB, and Lambda
Design the serverless architecture, create DynamoDB, configure API Gateway, and connect your first Lambda function.
Serverless architecture is one of the first AWS patterns that interested me because it allows an application to respond to requests without maintaining traditional application servers.
In this three-part series, I will build a complete serverless Task Management API using:
- Amazon API Gateway
- AWS Lambda
- Amazon DynamoDB
- AWS IAM
- Amazon CloudWatch
- AWS SAM
The final API will support creating, retrieving, updating, listing, and deleting tasks.
What We Are Building
Our API will expose these endpoints:
POST /tasksGET /tasksGET /tasks/{taskId}PUT /tasks/{taskId}DELETE /tasks/{taskId}
A task will look like this:
{ "taskId": "8aa22a11-7de4-4e65-a01d-2457ddbd8871", "title": "Learn AWS Lambda", "description": "Build my first serverless API", "status": "TODO", "createdAt": "2026-08-31T08:00:00+00:00"}
Architecture
The request flow is:
Internet | v +----------------+ | API Gateway | | HTTP API | +----------------+ | v +----------------+ | AWS Lambda | | Python API | +----------------+ | v +----------------+ | DynamoDB | | Tasks Table | +----------------+
CloudWatch will provide logging around the application.
Conceptually:
Client | | HTTPS vAPI Gateway | | Lambda proxy event vLambda | | AWS SDK vDynamoDB
API Gateway ----+ |Lambda ---------+----> CloudWatch
Why Use API Gateway?
Lambda functions are not HTTP servers by themselves.
API Gateway gives us a public HTTPS endpoint and converts incoming HTTP requests into events that Lambda can process.
For example:
POST /tasksContent-Type: application/json
{ "title": "Study DynamoDB", "description": "Learn partition keys"}
API Gateway forwards information about that request to Lambda.
With HTTP API payload format 2.0, Lambda receives an event containing information such as:
{ "version": "2.0", "routeKey": "POST /tasks", "rawPath": "/tasks", "requestContext": { "http": { "method": "POST", "path": "/tasks" } }, "body": "{\"title\":\"Study DynamoDB\"}"}
This means our Lambda function can inspect
routeKey and decide which operation should be executed.Why Lambda?
AWS Lambda executes code when an event occurs.
For our application:
HTTP request ↓API Gateway ↓Lambda invocation ↓Python code executes ↓DynamoDB operation ↓HTTP response
There is no EC2 instance that we need to patch or keep running continuously.
This also changes how we think about application design.
Instead of running a traditional web application process permanently, Lambda executes our handler when it receives a request.
Why DynamoDB?
DynamoDB is a serverless NoSQL database.
For our Task API, the access patterns are relatively simple.
We primarily need:
Create taskGet task by taskIdUpdate task by taskIdDelete task by taskId
We can therefore use:
Partition Key: taskId
An important DynamoDB lesson is that database design should normally start with the application's access patterns.
For this tutorial, the simple partition-key design is enough.
Later, we will discuss why
Scan is not necessarily the right choice for large production systems.
Step 1 - Create the DynamoDB Table
Open the DynamoDB console and create a table.
Use:
Table name:Tasks
Partition key:taskId
Type:String
For this project, no sort key is required.
Our table can contain items such as:
{ "taskId": "123", "title": "Learn Lambda", "description": "Understand Lambda event handling", "status": "TODO"}
Unlike a traditional relational table, DynamoDB does not require every item to contain exactly the same set of non-key attributes.
Step 2 - Create the Lambda Function
Create a Lambda function named:
tasks-api
Choose a currently supported Python runtime.
The basic Lambda handler starts as:
def lambda_handler(event, context): print(event)
return { "statusCode": 200, "body": "Hello from the Tasks API" }
At this point, Lambda does not yet have permission to access DynamoDB.
That leads us to an important AWS concept.
Step 3 - Understand the Lambda Execution Role
A Lambda function has an IAM execution role.
The execution role determines what AWS resources the function can access.
Our application requires operations such as:
dynamodb:PutItemdynamodb:GetItemdynamodb:UpdateItemdynamodb:DeleteItemdynamodb:Scan
A simplified IAM policy could look like:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:PutItem", "dynamodb:GetItem", "dynamodb:UpdateItem", "dynamodb:DeleteItem", "dynamodb:Scan" ], "Resource": "arn:aws:dynamodb:REGION:ACCOUNT_ID:table/Tasks" } ]}
Notice that we are not using:
"Action": "*","Resource": "*"
The goal is to give our application only the permissions it actually needs.
This is the principle of least privilege.
Step 4 - Configure the Table Name
We could hard-code the table name:
table = dynamodb.Table("Tasks")
But configuration is better kept separate from application code.
Create a Lambda environment variable:
TABLE_NAME=Tasks
Our Python code can then access it with:
import osimport boto3
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table( os.environ["TABLE_NAME"])
This makes it easier to reuse the same Lambda code across environments.
For example:
Development:TABLE_NAME=TasksDev
Production:TABLE_NAME=TasksProd
The application code does not need to change.
Step 5 - Understand the Request Lifecycle
Before writing our CRUD functions, it is worth understanding exactly what happens when someone calls the API.
Imagine the client sends:
GET /tasks/abc123
The lifecycle becomes:
1. Client sends HTTPS request
2. API Gateway receives: GET /tasks/abc123
3. API Gateway matches: GET /tasks/{taskId}
4. API Gateway invokes Lambda
5. Lambda receives: pathParameters.taskId = abc123
6. Lambda executes: DynamoDB GetItem
7. DynamoDB returns the item
8. Lambda creates an HTTP response
9. API Gateway returns it to the client
This separation is one of the key concepts behind serverless architectures.
API Gateway handles the HTTP layer.
Lambda handles application logic.
DynamoDB handles persistence.
IAM controls permissions between those components.
CloudWatch provides observability.
What We Have So Far
At this point, we have established the foundation for the application:
DynamoDB table +Lambda function +IAM permissions +Environment configuration
But our Lambda function does not yet perform any useful operations.
In Part 2, we will implement the complete CRUD API and connect Lambda to API Gateway.
We will build:
POST /tasksGET /tasksGET /tasks/{taskId}PUT /tasks/{taskId}DELETE /tasks/{taskId}
We will also look at request validation, DynamoDB conditional operations, HTTP status codes, and API testing.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article