AWS Builder Center

Building a Serverless URL Shortener with AWS Lambda, API Gateway, DynamoDB, and S3

Learn how to build a scalable serverless URL shortener using AWS services. This guide covers architecture, deployment, and best practices while keeping costs within the AWS Free Tier.

Building a Serverless URL Shortener on AWS

Introduction

Serverless computing allows developers to build scalable applications without managing servers. In this project, I built a URL Shortener using AWS services that automatically scales and stays within the AWS Free Tier.
The application accepts a long URL and returns a shortened version that redirects users to the original destination.



Architecture

The application uses the following AWS services:
  • Amazon API Gateway
  • AWS Lambda
  • Amazon DynamoDB
  • Amazon S3
  • AWS IAM
  • Amazon CloudWatch
Workflow:
User ↓ API Gateway ↓ AWS Lambda ↓ DynamoDB ↓ Return Short URL
When the short URL is opened:
User ↓ API Gateway ↓ Lambda ↓ DynamoDB ↓ Redirect to Original URL



Why Serverless?

Serverless applications provide several advantages:
  • No server management
  • Automatic scaling
  • Pay only for usage
  • High availability
  • Built-in security



Step 1: Create the DynamoDB Table

Create a table named:
URLMappings
Partition Key:
shortCode
Type:
String
The table stores:
  • Short Code
  • Original URL
  • Creation Date
  • Click Count



Step 2: Create the Lambda Function

The Lambda function performs two operations.
Generate URL
  • Receives original URL
  • Generates unique shortcode
  • Stores mapping in DynamoDB
  • Returns short URL
Redirect
  • Receives shortcode
  • Looks up DynamoDB
  • Returns HTTP redirect
  • Updates click counter



Step 3: Configure API Gateway

Create two endpoints.
POST /shorten
Creates a new shortened URL.
GET /{code}
Redirects users to the original website.



Step 4: Permissions

Create an IAM Role allowing Lambda to access:
  • DynamoDB
  • CloudWatch Logs
Always follow the principle of least privilege.



Step 5: Testing

Test using Postman or curl.
Example request:
POST /shorten
Example response:



Monitoring

Use Amazon CloudWatch to monitor:
  • Invocation count
  • Errors
  • Duration
  • Logs
This helps identify performance issues quickly.



Cost

This project fits comfortably within the AWS Free Tier for learning and portfolio purposes.
Services used:
  • Lambda
  • API Gateway
  • DynamoDB
  • CloudWatch
  • IAM



Improvements

Future enhancements could include:
  • Custom aliases
  • QR code generation
  • User authentication
  • Analytics dashboard
  • URL expiration
  • Rate limiting



What I Learned

This project improved my understanding of:
  • Event-driven architecture
  • Serverless development
  • REST APIs
  • IAM permissions
  • DynamoDB design
  • AWS monitoring



Conclusion

AWS makes it easy to build scalable applications without managing infrastructure. This project demonstrates how multiple AWS services integrate seamlessly to create a production-ready serverless application.
Happy Building!
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