This project implements a serverless URL shortener using AWS Lambda, DynamoDB, and API Gateway.
- Create a DynamoDB Table (to store URLs)
- Create IAM Role (for Lambda access)
- Create Two Lambda Functions (Shorten & Redirect)
- Set up API Gateway (to expose endpoints)
- Test using Postman
- Go to AWS Console → DynamoDB → Create Table
- Table name:
URLShortener
- Partition key:
short_code
(String) - Click Create Table
- Go to AWS IAM → Roles → Create Role
- Select AWS Service → Lambda
- Attach these policies:
AmazonDynamoDBFullAccess
AWSLambdaBasicExecutionRole
- Name it:
LambdaDynamoDBRole
- Click Create Role
This function generates a short URL and stores it in DynamoDB.
- Go to AWS Console → Lambda → Create Function
- Function name:
shorten_url
- Runtime: Python 3.9
- Execution role: Select
LambdaDynamoDBRole
- Click Create Function
- Replace the default code with the following:
import json
import boto3
import hashlib
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('URLShortener')
BASE_URL = "https://your-api-id.execute-api.region.amazonaws.com/" # Replace this later
def lambda_handler(event, context):
body = json.loads(event['body'])
long_url = body.get("long_url")
if not long_url:
return {"statusCode": 400, "body": json.dumps({"error": "Missing long_url"})}
short_code = hashlib.md5(long_url.encode()).hexdigest()[:6]
table.put_item(Item={"short_code": short_code, "long_url": long_url})
short_url = BASE_URL + short_code
return {
"statusCode": 200,
"body": json.dumps({"short_url": short_url})
}
- Click Deploy
This function retrieves the long URL from DynamoDB and redirects users.
- Go to AWS Console → Lambda → Create Function
- Function name:
redirect_url
- Runtime: Python 3.9
- Execution role: Select
LambdaDynamoDBRole
- Click Create Function
- Replace the default code with:
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('URLShortener')
def lambda_handler(event, context):
short_code = event["pathParameters"]["short_code"]
response = table.get_item(Key={"short_code": short_code})
if "Item" not in response:
return {"statusCode": 404, "body": json.dumps({"error": "URL not found"})}
long_url = response["Item"]["long_url"]
return {
"statusCode": 301,
"headers": {"Location": long_url}
}
- Click Deploy
- Go to AWS Console → API Gateway → Create API
- Select "HTTP API"
- Click Build
- Name:
URLShortenerAPI
- Click Create API
- Go to Routes → Click Create
- Method:
POST
- Path:
/shorten
- Click Create
- Go to Integrations → Attach a Lambda Function
- Select
shorten_url
- Click Deploy
- Copy API Gateway URL and update
BASE_URL
inshorten_url
Lambda function.
- Go to Routes → Click Create
- Method:
GET
- Path:
/{short_code}
- Click Create
- Go to Integrations → Attach a Lambda Function
- Select
redirect_url
- Click Deploy
- Method:
POST
- URL:
https://your-api-id.execute-api.region.amazonaws.com/shorten
- Headers:
{ "Content-Type": "application/json" }
- Body (JSON):
{ "long_url": "https://www.google.com" }
✅ Response (Example):
{"short_url": "https://your-api-id.execute-api.region.amazonaws.com/abc123"}
- Method:
GET
- URL:
https://your-api-id.execute-api.region.amazonaws.com/abc123
✅ If successful, it should redirect to https://www.google.com
.
✅ DynamoDB Table → Stores short_code
→ long_url
✅ Lambda Functions →
shorten_url
(POST/shorten
) → Generates & stores short URLsredirect_url
(GET/{short_code}
) → Fetches & redirects ✅ API Gateway → Exposes the endpoints ✅ Postman Testing → Confirm endpoints work
🚀 Your URL shortener is now live! 🎉