
Build Your First Serverless REST API with AWS Lambda, API Gateway, and DynamoDB
Learn how to build a simple serverless REST API using Amazon API Gateway, AWS Lambda, and Amazon DynamoDB through a practical step-by-step project for beginners.
Build Your First Serverless REST API with AWS Lambda, API Gateway, and DynamoDB
Building a small project is one of the best ways to understand how AWS services work together.
In this tutorial, we will build a simple serverless REST API using:
- Amazon API Gateway
- AWS Lambda
- Amazon DynamoDB
By the end of this tutorial, you will understand how an HTTP request travels through API Gateway, reaches Lambda, and interacts with DynamoDB.
Architecture
Our application will follow this architecture:
Client | vAmazon API Gateway | vAWS Lambda | vAmazon DynamoDB
API Gateway handles the HTTP request, Lambda runs the application logic, and DynamoDB stores our data.
Prerequisites
Before starting, you need:
- An AWS account
- Basic knowledge of the AWS Management Console
- Basic understanding of JSON
- A web browser
For this tutorial, we will use the AWS Management Console.
Step 1: Create a DynamoDB Table
First, we will create a DynamoDB table to store user information.
1. Open DynamoDB
Open the AWS Management Console and search for:
DynamoDB
2. Create the Table
Select Create table and use the following values:
Table name:
Users
Partition key:
id
Data type:
String
Then select Create table.
Our data will contain fields such as:
Users | +-- id +-- name +-- email
Step 2: Add Sample Data
Open the Users table and go to the Items section.
Create a sample item:
{ "id": "101", "name": "Vasu", "email": "vasu@example.com"}
Now our DynamoDB table contains a sample user.
Step 3: Create an AWS Lambda Function
Next, we will create the backend logic using AWS Lambda.
Open the AWS Lambda service from the AWS Management Console.
Select:
Create function
Choose:
Author from scratch
Use the following function name:
UsersApiFunction
Select a supported Python runtime and create the function.
Step 4: Test the Lambda Function
Replace the default Lambda code with:
import json
def lambda_handler(event, context):
return { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": json.dumps({ "message": "Hello from my serverless API!" }) }
Save the function and use the Test feature.
A successful response should look similar to:
{ "statusCode": 200, "body": "{\"message\": \"Hello from my serverless API!\"}"}
This confirms that our Lambda function is working.
Step 5: Give Lambda Permission to Access DynamoDB
Our Lambda function needs permission to communicate with DynamoDB.
Lambda uses an IAM execution role for permissions.
Open the execution role associated with the Lambda function and provide only the permissions required for this project.
For example, the application may need permissions such as:
-
GetItem
-
PutItem
-
UpdateItem
-
DeleteItem
Avoid giving unnecessary permissions.
This follows the principle of least privilege.
Step 6: Connect Lambda to DynamoDB
Now let's update our Lambda function so that it can retrieve a user from DynamoDB.
Use the following example:
import jsonimport boto3
dynamodb = boto3.resource("dynamodb")table = dynamodb.Table("Users")
def lambda_handler(event, context):
user_id = event.get("pathParameters", {}).get("id")
response = table.get_item( Key={ "id": user_id } )
item = response.get("Item")
if not item: return { "statusCode": 404, "body": json.dumps({ "message": "User not found" }) }
return { "statusCode": 200, "body": json.dumps(item) }
The function receives the user ID from the API request and searches for that ID in DynamoDB.
For example:
/users/101
The Lambda function extracts
101 and uses it to retrieve the corresponding item from DynamoDB.
Step 7: Create an API Gateway API
Now we need an HTTP endpoint for our Lambda function.
Open Amazon API Gateway.
Create an HTTP API and add the following route:
GET /users/{id}
Connect this route to:
UsersApiFunction
Now a request such as:
GET /users/101
can invoke our Lambda function.
Step 8: Deploy the API
After configuring the API route and Lambda integration, deploy the API.
API Gateway will provide an invoke URL.
It will look similar to:
https://YOUR_API_ID.execute-api.YOUR_REGION.amazonaws.com
Your actual URL will be different.
Our endpoint will look like:
GET https://YOUR_API_URL/users/101
Step 9: Test the API
Now let's test our API.
You can use a browser, Postman, curl, or another HTTP client.
For example:
curl https://YOUR_API_URL/users/101
If the user exists, the API can return:
{ "id": "101", "name": "Vasu", "email": "vasu@example.com"}
The complete request flow is:
Client | | GET /users/101 vAPI Gateway | vLambda | | GetItem vDynamoDB | vLambda | vAPI Gateway | vClient
This is the main concept behind our serverless application.
Step 10: Add More API Operations
Once the GET operation is working, we can extend the project into a CRUD API.
For example:
POST /usersGET /users/{id}PUT /users/{id}DELETE /users/{id}
These operations can be connected to Lambda functions that perform the corresponding DynamoDB operations.
This gives us a basic serverless backend.
Step 11: Add Error Handling
A production application should handle errors properly.
For example, if a requested user does not exist, return:
{ "message": "User not found"}
with an appropriate HTTP status code.
You should also validate input and handle unexpected application errors.
Step 12: Monitor the Application
Monitoring is an important part of building cloud applications.
For Lambda, review the available logs and metrics when troubleshooting.
A simple troubleshooting flow is:
API Request | vAPI Gateway | vLambda | +---- Error? | v Check Logs
If an API request fails, checking the Lambda logs is a useful first step.
Common Problems and Solutions
Lambda Cannot Access DynamoDB
Check the Lambda execution role and verify that it has the required DynamoDB permissions.
API Returns 404
Check:
- API route
- HTTP method
- API deployment
- Requested user ID
Lambda Returns an Error
Check the Lambda logs.
Also verify:
- DynamoDB table name
- Partition key
- Request structure
- Lambda code
Step 13: Clean Up AWS Resources
After completing the tutorial, delete resources that you no longer need.
For example:
- API Gateway API
- Lambda function
- DynamoDB table
Cleaning up resources is a good habit when experimenting with AWS.
Always review your AWS resources and billing information so that you understand what is running in your account.
Final Architecture
After completing this project, our application looks like:
Client | vAPI Gateway | vLambda | vDynamoDB
Each service has a specific responsibility.
Amazon API Gateway
Handles HTTP requests and routes them to the backend.
AWS Lambda
Runs application logic without requiring us to manage traditional servers.
Amazon DynamoDB
Stores and retrieves application data.
What I Learned
This project helped me understand:
- Serverless architecture
- API Gateway routes
- Lambda functions
- DynamoDB tables
- IAM permissions
- HTTP APIs
- CRUD operations
- Monitoring and troubleshooting
The most important takeaway is understanding how these services work together.
Instead of thinking:
Lambda = ComputeAPI Gateway = APIDynamoDB = Database
we can understand the complete flow:
HTTP Request | vAPI Gateway | vLambda | vDynamoDB | vHTTP Response
Next Steps
Once this basic project is working, you can extend it by adding:
- Authentication and authorization
- Input validation
- Better error handling
- CloudWatch monitoring
- A frontend application
- CI/CD
- Infrastructure as Code
- Additional API endpoints
You can also turn this simple project into a complete serverless application.
Conclusion
Building projects is a great way to move from AWS theory to practical cloud development.
This serverless REST API is a simple starting point for understanding how multiple AWS services can work together.
If you are learning AWS, try building this project yourself and then customize it with your own features.
Happy building! 🚀
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article