URL Shortener - AWS Serverless Microservice
📌 Table of Contents
1. Overview
2. Architecture
3. AWS Services Used
4. Lambda Functions
○ RedirectShortURL
○ CreateShortURL
5. DynamoDB Tables
○ ShortURLs
○ URLCounter
6. API Gateway Setup
7. IAM Permissions
8. Testing the APIs
9. Frontend Deployment
10. Screenshots
🌐 Overview
This documentation describes a URL Shortener Service built using AWS Lambda,
DynamoDB, and API Gateway. The service allows users to:
● Generate short URLs from long URLs.
● Redirect short URLs to their original long URLs.
● Track URL usage via a counter.
The frontend is built with HTML, CSS, and JavaScript, deployed on Vercel with a
Node.js backend.
✨ Architecture
The system follows this flow:
1. User submits a long URL → CreateShortURL Lambda generates a short code
and stores it in DynamoDB.
2. User accesses short URL → RedirectShortURL Lambda fetches the original
URL and redirects.
3. DynamoDB tables (ShortURLs, URLCounter) store mappings and count visits.
4. API Gateway exposes endpoints:
○ POST /shorten → Create a short URL.
○ GET /{short_code} → Redirect to original URL.
5. Frontend (hosted on Vercel) interacts with the API.
💻 AWS Services Used
● AWS Lambda (Python)
● DynamoDB (NoSQL Database)
● API Gateway (REST API)
● IAM (Permissions Management)
☁️ Lambda Functions
RedirectShortURL
Purpose: Fetches the original URL from a short code and redirects.
Trigger: API Gateway (GET /{short_code})
Python Code Snippet
python
def lambda_handler(event, context):
path_parameters = event.get('pathParameters', {})
if not path_parameters or not isinstance(path_parameters, dict):
path_parameters = {}
short_code = path_parameters.get('short_code')
CreateShortURL
Purpose: Generates a short code for a given long URL and stores it in DynamoDB.
Trigger: API Gateway (POST /shorten)
Python Code Snippet
python
def generate_short_code():
response = counter_table.update_item(
Key={'counter_name': 'url_counter'},
UpdateExpression='ADD counter_value :incr',
ExpressionAttributeValues={':incr': 1},
ReturnValues='UPDATED_NEW'
counter = response['Attributes']['counter_value']
# Create a shorter code with base62-like encoding
short_code = base64.urlsafe_b64encode(
str(counter).encode()
).decode().rstrip('=')
return short_code, None
⚙️ DynamoDB Tables
ShortURLs
Attribute Type Description
short_code String Primary Key (Short URL
ID)
long_url String Original URL
URLCounter
Attribute Type Description
counter_name String Primary Key
("url_counter")
count_value Number Total URLs generated
📈 API Gateway Setup
● API Name: URLShortener
● Resources:
○ GET /{short_code} → Triggers RedirectShortURL
○ POST /shorten → Triggers CreateShortURL
● CORS: Enabled for both endpoints.
🧑 IAM Permissions
● Created a custom IAM policy for Lambda to access DynamoDB.
● Attached to Lambda execution role.
Policy:
json
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:Query",
"dynamodb:Scan"
],
"Resource": [
"arn:aws:dynamodb:region:id:table/ShortURLs",
"arn:aws:dynamodb:region:id::table/URLCounter"
]
}
]
}
🧪 Testing the APIs
Bash Commands
bash
# Create Short URL
curl -X POST \
https://rcefn1q34m.execute-api.eu-central-1.amazonaws.com/prod/shorten \
-H "Content-Type: application/json" \
-d '{"url":"https://example.com"}'
💻 Frontend Deployment
● Built with HTML, CSS, JavaScript.
● Node.js server (server.js) for local testing.
● Deployed on Vercel
● Accessible at: https://url-shortener-nu-lilac.vercel.app/
📷 Screenshots
AWS Lambda Functions
DynamoDB Tables
API Gateway Setup
Live Demo: Vercel Link
📜 License
MIT © [Abdullah Siraj] | GitHub | LinkedIn