Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

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.

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

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.

HTTP API to Lambda to DynamoDB table with PK shortCode, attributes longUrl and clicks. POST creates. GET returns 302 and ADD clicks.

Request pathClientPOST /shortenAPI Gateway HTTP APILambdaPutItemClientGET /Ab12XyZLambdaGetItem + ADD clicks302
Same idea as the Notes API lab.

Download: url-shortener-app.zip — Python handler.py (boto3 built-in), sample IAM.

2. Architecture

Start with the two jobs this application must perform. The create request sends a long address and gets a new short code. The open request sends just that short code and needs the original address back. A route is the combination of an HTTP method and a path that API Gateway uses to choose where to send the request. Here both routes call Lambda, the service running the Python handler. The handler decides whether to save or look up an item in DynamoDB.

Read a sample item as three named values. shortCode=Ab12XyZ identifies it, longUrl=https://example.com/lesson stores the destination, and clicks=0 starts the count. The code is the partition key, the value supplied to find that item directly. A visitor who opens /Ab12XyZ does not need to know the table name or the destination. A code that does not exist has no address to redirect to and should produce a missing-code response.

PieceServiceWhy
APIAPI Gateway HTTP APIHTTPS + CORS
LogicLambda Python 3.12boto3 included
DataDynamoDB on-demandKeyed by shortCode

3. Lab: deploy click by click

Region ap-south-1. Replace yourname.

A. DynamoDB

Before configuring any service, write two sample requests on paper. For POST /shorten, the input is {"url":"https://example.com/report"}. The reply should contain a code and a usable short address. A new item holds that code, the destination and an initial click count. For GET /Ab12XyZ, there is no JSON body to submit. The value after the slash is the key the code must look up.

Now walk through a missing key. The handler searches its table and gets no stored item. A redirect in this case would send the visitor to an invented address. The appropriate response is a not-found status. This gives you two lab checks: test a code you own and a made-up code that was never stored. The successful create test exercises a write; one open request tests a read and a counter update.

TABLE_NAME is a Lambda environment variable, which is configuration supplied to the program at runtime. It connects the handler to the table you created without changing the Python source. Compare that name with the table named in the role policy. If the name is wrong, changing the route alone will not fix storage.

  1. Create table yourname-urls. PK shortCode (String). On-demand. No sort key.
  2. Wait until Active.

B. IAM role

  1. Create role for Lambda. Attach AWSLambdaBasicExecutionRole. Name yourname-urls-role.
  2. Inline policy from the zip: PutItem, GetItem, UpdateItem on that table only. Replace ACCOUNT_ID.

C. Lambda

  1. Create function yourname-urls-api, Runtime Python 3.12, existing role.
  2. Paste lambda/handler.py. Handler handler.lambda_handler.
  3. Env TABLE_NAME=yourname-urls. Memory 256 MB, timeout 10 s. Deploy.

D. HTTP API

  1. Create HTTP API yourname-urls-http, integrate the Lambda.
  2. Routes: POST /shorten, GET /{code}.
  3. CORS origin *, methods GET,POST,OPTIONS.
  4. Copy Invoke URL as API_BASE. Set Lambda env BASE_URL=API_BASE (no slash). Deploy again.

E. Test

Save the code from the create response before testing the open route. A 201 is the HTTP status for a created resource. On the next request, 302 tells the client to redirect and the Location header supplies the address. Headers are response information separate from the body. Inspect them with the command below, then check the click field for that same code in the table. These are two independent checks.

Imagine two visitors arrive when the count is 7. If two workers each read 7 and later write 8, one visit is lost. With DynamoDB’s numeric ADD, each update increments the stored value in the service. Two successful updates add two to the original count. This counts successful redirect processing requests, not individual people.

curl -s -X POST "$API_BASE/shorten" -H 'content-type: application/json' \
  -d '{"url":"https://ravindrabagale.com/aws/"}'
curl -sS -D - -o /dev/null "$API_BASE/YOURCODE"

Expect 201 JSON with shortCode, then 302 Location to the long URL. DynamoDB clicks should be at least 1.

Use a fresh short code and issue one GET. Look for 302 and the expected Location in the response headers, then read clicks on that short-code row. -D - shows the headers and -o /dev/null discards the body. The command leaves redirect following off, so it checks the API response without requesting the destination.

Cleanup

  1. Delete HTTP API, Lambda, DynamoDB table, IAM role.
  2. 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

  1. Add GET /stats/{code} returning clicks without redirect.
  2. Allow only https URLs.

In short

  1. Shortener = HTTP API + Lambda + DynamoDB.
  2. POST creates; GET redirects and counts.
  3. Least-privilege IAM.
  4. Clean up.

Follow the redirect, then verify the row

Run the lab with a synthetic HTTPS destination. Capture the generated short code, inspect the 302 and Location without following it, then check the click counter on the same row. In the Python archive the entry point is handler.lambda_handler.

Interview check What happens if two randomly generated IDs match?

Create the mapping with an attribute_not_exists condition and retry with a fresh candidate after a conditional conflict. The simple archive does not include that protection. Keep both routes private in a lab, and plan destination validation, abuse handling and expiration before creating a public shortener.

Reference documentation

Practise this chapter

Interview questions and answers

How would you demonstrate that both the URL mapping and the click counter worked?

I would create a mapping and note the short code, request that code without following the redirect, then check the 302 and Location header. I would read the same DynamoDB item to verify the counter. Checking only the redirect cannot establish that the table update succeeded.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. The create call returned shortCode=Ab12XyZ. What should a visitor request to open the address?

Answer and explanation

B. GET /Ab12XyZ

The create route takes the long URL. The GET route looks up the code supplied in its path.

2. Which response evidence tells a browser where the short link leads?

Answer and explanation

A. A 302 response and a Location header

302 signals a redirect. Location holds the address to follow.

3. Two visitors arrive when clicks=7. Which approach avoids losing one increment?

Answer and explanation

C. Use DynamoDB ADD for each increment

ADD increments the stored number atomically. Two successful additions yield 9.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

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

See which functions exist in this Region.

List the Lambda functions in eu-west-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Invoke a function

Invoke runs the function now and saves its answer in a file.

Invoke lisboatiles-orders-fn and save the response to out.json.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Trust policy for a Lambda role

The trust policy says WHO may take the role.

Write a trust policy that lets lambda.amazonaws.com assume the role.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Put an item

DynamoDB JSON shows the type of each value: S for string, N for number.

Put an item into lisboatiles-orders with orderId "o-101" (string) and amount 2500 (number).

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Change timeout and memory

More memory also gives more CPU; timeout stops runaway runs.

Set lisboatiles-orders-fn to timeout 30 seconds and memory 512 MB.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Let a function write logs

Without these actions, Lambda logs never appear in CloudWatch.

Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Let S3 call your function

A resource-based permission is needed before S3 can invoke the function.

Add permission statement s3invoke to lisboatiles-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::lisboatiles-uploads.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Quick-create an HTTP API for Lambda

Quick create builds an HTTP API, a default route and stage pointing at your function.

Create an HTTP API named lisboatiles-api whose target is arn:aws:lambda:eu-west-1:101273714098:function:lisboatiles-orders-fn.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create a Lambda function

Lambda needs the code zip, a runtime, a handler name and an execution role.

Create function lisboatiles-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::101273714098:role/lambda-basic, code in function.zip.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Set environment variables

Keep settings like table names out of the code.

Set the environment variable TABLE=lisboatiles-orders on lisboatiles-orders-fn.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Invoke with a JSON payload

In CLI v2, raw JSON needs --cli-binary-format raw-in-base64-out.

Invoke lisboatiles-orders-fn with payload {"orderId":"o-101"} (raw JSON) and save the reply in out.json.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Lambda GB-seconds

Lambda bills memory × time.

1,000,000 invocations, 512 MB memory, 200 ms each. How many GB-seconds?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. 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.

Revision card: What is a URL shortener?. Instagram bios and Flipkart share links cannot paste a huge URL. People need a tiny code that opens the long page. A URL shortener stores a long URL behind a short code (like Ab12XyZ). Visiting the short link redirects to the long URL and can count clicks. Build it with API Gateway (HTTPS), Lambda (logic), and DynamoDB (shortCode → longUrl, clicks). No always-on EC2. POST {"url":"https://ravindrabagale.com/aws/"} returns shortCode Ab12XyZ. GET /Ab12XyZ returns HTTP 302 to that long URL.
Revision card: What is a URL shortener? · Download PNG · Open card page
Revision card: URL shortener architecture. Pieces must fit: front door, compute, and keyed storage for millions of codes. HTTP API routes → one Lambda → DynamoDB table with partition key shortCode (String). Attributes: longUrl, clicks. Create table on-demand, IAM role with Put/Get/UpdateItem, paste handler, add POST /shorten and GET /{code}, set TABLE_NAME and BASE_URL. curl POST /shorten then curl -I /CODE shows Location: https://ravindrabagale.com/aws/ and clicks becomes 1.
Revision card: URL shortener architecture · Download PNG · Open card page
Revision card: Why 302 and ADD clicks?. Returning 200 with the URL in JSON forces every client to invent redirect logic. Browsers already follow Location. HTTP 302 Found with a Location header is the classic shortener response. ADD on clicks is an atomic DynamoDB increment under concurrent hits. In Lambda: GetItem by shortCode → UpdateItem ADD clicks :1 → return statusCode 302 and headers Location = longUrl. Two phones open /Ab12XyZ at once; both redirect and clicks becomes 2, not a lost update.
Revision card: Why 302 and ADD clicks? · Download PNG · Open card page
Revision card: Clean up the shortener lab. Forgotten HTTP APIs and DynamoDB tables clutter the account and can surprise you on the bill. Delete in order: API Gateway HTTP API, Lambda function, DynamoDB table, IAM role (and inline policy). API Gateway console › delete API; Lambda › delete; DynamoDB › delete table; IAM › delete role. Confirm Billing free-tier noise only. After cleanup, curl to the old invoke URL fails; Cost Explorer shows near-zero for the lab.
Revision card: Clean up the shortener lab · Download PNG · Open card page
Comparison: JSON 200 with URL vs HTTP 302 redirect. JSON 200 with URL: Client reads JSON and navigates itself. HTTP 302 redirect: Browser follows Location automatically.
Comparison: JSON 200 with URL vs HTTP 302 redirect · Download PNG · Open card page
Interview card: Why return 302 instead of 200 with the long URL?. Browsers automatically follow the Location header. That is what users expect from a short link. A JSON 200 forces every client (browser, WhatsApp preview, curl scripts) to special-case your API. Lambda returns statusCode 302 and headers Location = longUrl from DynamoDB GetItem.
Interview card: Why return 302 instead of 200 with the long URL? · Download PNG · Open card page
Interview card: Why ADD clicks instead of read-modify-write?. Two redirects at once can both read clicks=5 and both write 6 — one click lost. UpdateItem ADD is atomic on the server for that attribute. UpdateExpression SET … ADD clicks :one with :one = 1 on every redirect.
Interview card: Why ADD clicks instead of read-modify-write? · Download PNG · Open card page
Interview card: GetItem or Scan to resolve a short code?. Always GetItem by partition key shortCode. Scan would read the whole table. Shortener traffic is a classic single-key lookup pattern. PK shortCode String; GetItem Key={shortCode: code} in under 10 ms typically.
Interview card: GetItem or Scan to resolve a short code? · Download PNG · Open card page

More cards: Ravi Sir's Cards.