AWS Builder Center

Build a Serverless URL Shortener with AWS Lambda and DynamoDB

Learn how to build a simple, scalable URL shortener using AWS Lambda, Amazon DynamoDB, and Amazon API Gateway without managing servers.

# Build a Serverless URL Shortener with AWS Lambda and DynamoDB
URL shorteners are a great beginner project for understanding how serverless applications work on AWS.
In this tutorial, we'll build a simple URL shortener using:
- **Amazon API Gateway** – Provides HTTP endpoints
- **AWS Lambda** – Runs our application logic
- **Amazon DynamoDB** – Stores shortened URLs
- **Amazon CloudWatch** – Helps monitor Lambda execution
## Architecture
The application follows this simple flow:
```text
User
|
v
API Gateway
|
v
AWS Lambda
|
v
DynamoDB
When a user submits a long URL, Lambda generates a short ID and stores the mapping in DynamoDB.
For example:
Long URL:
https://example.com/my-long-web-page
Short URL:
https://your-api-url/abc123
Step 1: Create a DynamoDB Table
Create a DynamoDB table named:
UrlShortener
Use the following partition key:
shortId
Set the key type to:
String
The table can contain items such as:
{
"shortId": "abc123",
"longUrl": "https://example.com/my-long-web-page"
}
Step 2: Create the Lambda Function
Create a Lambda function using Python.
The function receives a URL, generates a unique short ID, and stores the data in DynamoDB.
Example:
import json
import boto3
import uuid
table = boto3.resource("dynamodb").Table("UrlShortener")
def lambda_handler(event, context):
body = json.loads(event["body"])
long_url = body["url"]
short_id = str(uuid.uuid4())[:6]
table.put_item(
Item={
"shortId": short_id,
"longUrl": long_url
}
)
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"shortId": short_id,
"url": long_url
})
}
Step 3: Give Lambda Permission to Access DynamoDB
Lambda needs permission to write data into DynamoDB.
Create or use an IAM execution role and attach an appropriate policy that allows the required DynamoDB operation.
For production applications, follow the principle of least privilege instead of granting unnecessary permissions.
Step 4: Create an API Gateway Endpoint
Create an API Gateway HTTP API and connect it to the Lambda function.
For example:
POST /shorten
The client can send:
{
"url": "https://example.com/my-long-web-page"
}
The Lambda function generates a short ID and saves the URL in DynamoDB.
Step 5: Retrieve the Original URL
We can also create another endpoint:
GET /{shortId}
When someone visits:
GET /abc123
Lambda can search DynamoDB for abc123.
If the URL exists, the application can return a redirect response.
The flow becomes:
Browser
|
v
API Gateway
|
v
Lambda
|
v
DynamoDB
|
v
Original URL
Why Serverless?
The biggest advantage of this architecture is that we don't need to manage servers.
AWS Lambda automatically runs our code when requests arrive, while DynamoDB provides a managed database.
This makes the application:
Easy to deploy
Highly scalable
Suitable for small projects
Cost-efficient for low traffic
Easy to extend
Important Security Considerations
When building a real-world URL shortener, don't rely only on the basic example.
Consider adding:
URL validation
Authentication if required
Rate limiting
Input size limits
IAM least-privilege permissions
Logging and monitoring
Protection against malicious URLs
Possible Improvements
Once the basic application works, we can extend it with:
Custom short URLs
Expiration dates
Click analytics
QR code generation
User authentication
A web frontend
Custom domains
Amazon CloudFront integration
Conclusion
A URL shortener is a small project, but it demonstrates several important AWS concepts.
By combining API Gateway, AWS Lambda, and DynamoDB, we can build a complete backend without provisioning or managing traditional servers.
Projects like this are a great way to understand serverless architecture and start building practical applications on AWS.
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