Project: Build a URL Shortener with Lambda, API Gateway and DynamoDB (Source Code)
Like bit.ly or WhatsApp short links: a tiny code opens a long URL. Build it with API Gateway, Lambda and DynamoDB — same stack as the Notes project. Download the zip, deploy in ap-south-1, test redirects, then clean up.
What you'll learn in this chapter
- Design shortCode PK and click counter
- Create table, IAM, Lambda, HTTP API
- POST /shorten and GET /{code} redirect
- Verify with curl and clean up
1. Everyday example first
Why do we need this?
Instagram bio and Flipkart share links cannot paste a huge URL. A shortener stores the long URL, returns a 7-character code, and on each visit redirects and counts clicks. No EC2.
What is it?
HTTP API to Lambda to DynamoDB table with PK shortCode, attributes longUrl and clicks. POST creates. GET returns 302 and ADD clicks.
Download: url-shortener-app.zip — Python handler.py (boto3 built-in), sample IAM.
2. Architecture
| Piece | Service | Why |
|---|---|---|
| API | API Gateway HTTP API | HTTPS + CORS |
| Logic | Lambda Python 3.12 | boto3 included |
| Data | DynamoDB on-demand | Keyed by shortCode |
3. Lab: deploy click by click
How to do it (step by step)
Region ap-south-1. Replace yourname.
A. DynamoDB
- Create table
yourname-urls. PKshortCode(String). On-demand. No sort key. - Wait until Active.
B. IAM role
- Create role for Lambda. Attach
AWSLambdaBasicExecutionRole. Nameyourname-urls-role. - Inline policy from the zip: PutItem, GetItem, UpdateItem on that table only. Replace ACCOUNT_ID.
C. Lambda
- Create function
yourname-urls-api, Runtime Python 3.12, existing role. - Paste
lambda/handler.py. Handlerhandler.lambda_handler. - Env
TABLE_NAME=yourname-urls. Memory 256 MB, timeout 10 s. Deploy.
D. HTTP API
- Create HTTP API
yourname-urls-http, integrate the Lambda. - Routes:
POST /shorten,GET /{code}. - CORS origin
*, methods GET,POST,OPTIONS. - Copy Invoke URL as
API_BASE. Set Lambda envBASE_URL=API_BASE(no slash). Deploy again.
E. Test
curl -s -X POST "$API_BASE/shorten" -H 'content-type: application/json' \
-d '{"url":"https://ravindrabagale.com/aws/"}'
curl -sI "$API_BASE/YOURCODE" | head -5
Expect 201 JSON with shortCode, then 302 Location to the long URL. DynamoDB clicks should be at least 1.
Cleanup
- Delete HTTP API, Lambda, DynamoDB table, IAM role.
- Check Billing for tiny / free-tier charges only.
4. Interview angles
Q. Why 302 not 200 with URL in body?
Browsers follow Location; that is a shortener.
Q. Why ADD clicks?
Atomic under concurrent redirects.
Q. GetItem vs Scan for redirect?
Always GetItem by PK.
Q. Custom domain?
ACM + API custom domain or CloudFront.
Q. Cleanup list?
API, Lambda, table, role.
Try at home
- Add GET /stats/{code} returning clicks without redirect.
- Allow only https URLs.
In short
- Shortener = HTTP API + Lambda + DynamoDB.
- POST creates; GET redirects and counts.
- Least-privilege IAM.
- Clean up.
Practice Tasks
Lisboa Tiles is a ceramic tiles business in Lisbon. João Silva looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-west-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 12
Easy List functions
Why: See which functions exist in this Region.
What to do: List the Lambda functions in eu-west-1.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Invoke a function
Why: Invoke runs the function now and saves its answer in a file.
What to do: Invoke lisboatiles-orders-fn and save the response to out.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Trust policy for a Lambda role
Why: The trust policy says WHO may take the role.
What to do: Write a trust policy that lets lambda.amazonaws.com assume the role.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Put an item
Why: DynamoDB JSON shows the type of each value: S for string, N for number.
What to do: Put an item into lisboatiles-orders with orderId "o-101" (string) and amount 2500 (number).
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Change timeout and memory
Why: More memory also gives more CPU; timeout stops runaway runs.
What to do: Set lisboatiles-orders-fn to timeout 30 seconds and memory 512 MB.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Let a function write logs
Why: Without these actions, Lambda logs never appear in CloudWatch.
What to do: Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Hard Let S3 call your function
Why: A resource-based permission is needed before S3 can invoke the function.
What to do: Add permission statement s3invoke to lisboatiles-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::lisboatiles-uploads.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Quick-create an HTTP API for Lambda
Why: Quick create builds an HTTP API, a default route and stage pointing at your function.
What to do: Create an HTTP API named lisboatiles-api whose target is arn:aws:lambda:eu-west-1:101273714098:function:lisboatiles-orders-fn.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Create a Lambda function
Why: Lambda needs the code zip, a runtime, a handler name and an execution role.
What to do: Create function lisboatiles-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::101273714098:role/lambda-basic, code in function.zip.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Set environment variables
Why: Keep settings like table names out of the code.
What to do: Set the environment variable TABLE=lisboatiles-orders on lisboatiles-orders-fn.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Invoke with a JSON payload
Why: In CLI v2, raw JSON needs --cli-binary-format raw-in-base64-out.
What to do: Invoke lisboatiles-orders-fn with payload {"orderId":"o-101"} (raw JSON) and save the reply in out.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Lambda GB-seconds
Why: Lambda bills memory × time.
What to do: 1,000,000 invocations, 512 MB memory, 200 ms each. How many GB-seconds?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Revision, comparison and interview cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.








More cards: Ravi Sir's Cards.