Build Your Own URL Shortener with AWS!

Build Your Own URL Shortener with AWS!

Written by Zackary Frazier, posted on

A URL shortener is the "Hello, World" of system design. They are deceptively simple on the surface, yet packed with real-world engineering trade-offs. On paper, it's just two endpoints: one to save a link, and another to redirect it. In practice, building one forces you to decide how to mint short codes, how to cache a read-heavy redirect path, and how much junk you can refuse before it ever reaches your backend.

Recently, I decided to move past the whiteboard diagrams and actually build and deploy my own cloud-native URL shortener from scratch. The stack is defined in AWS CDK, and the source is on GitHub. Here is what went into the architecture, the trade-offs I ran into, and what I learned along the way.

The first design decision was choosing the right platform, and I went with AWS based on my past experience building in its ecosystem. Once the platform was set, the next question was scope—how far should I take a classic system design exercise?

To keep the project grounded in real-world engineering constraints rather than stopping at a quick prototype, I set four core requirements:

  • Internal write path: Link registration is designed as an internal service rather than a public, customer-facing form.
  • IAM-based security: To keep authentication simple and avoid maintaining custom token logic, access control is handled natively through AWS IAM.
  • Custom production domain: Instead of relying on raw auto-generated API Gateway URLs, the service had to live behind a real custom domain: https://shortlink.hakt.tech.
  • AWS best practices: Every piece of the stack—from least-privilege permissions to HTTPS encryption and infrastructure-as-code—needed to align with production-grade AWS patterns.

The read path

While the POST endpoint is locked down with IAM authentication to protect writes, the read path (GET /{token}) is completely public and built for speed. Because redirects make up the vast majority of traffic in a URL shortener, this path needs to do as little work as possible.

When a request reaches the read Lambda, the work is a single primary-key lookup:

  • Hit: If the token exists, the service returns an HTTP 301 Moved Permanently response with the destination URL in the Location header, and the browser completes the navigation.
  • Miss: If the token isn't found, it returns a 404 with an HTML error page rather than a raw JSON body.

Why 301 matters here: Choosing a 301 over a 302 (Found) is a classic system design trade-off. A 301 tells browsers and CloudFront that the redirect is permanent, so repeat visits can be served from cache without calling Lambda or DynamoDB again. That keeps latency low and protects the database from traffic spikes, at the cost of click tracking and the ability to change a link's target later. Browsers remember a 301 aggressively, so a wrong destination sticks.

The domain and the edge

To keep the write path (POST /) locked down without custom auth logic, callers sign each request with AWS Signature Version 4 (SigV4). API Gateway checks that signature against IAM, so only principals with execute-api:Invoke can create a link. The viewer-request function lets non-GET methods through, which means a signed POST to https://shortlink.hakt.tech is how new links are created. The same hostname serves both routes.

While my domain's DNS is hosted on Cloudflare, I wanted the edge security, TLS termination, and caching to live inside AWS. Here is how https://shortlink.hakt.tech is wired:

  • DNS-only routing via Cloudflare: Instead of enabling Cloudflare's reverse proxy (the "orange cloud"), I set the shortlink CNAME to DNS-only (grey cloud) and pointed it at the Amazon CloudFront distribution. Passing traffic straight through avoids double-proxying, and TLS terminates at AWS.
  • TLS with AWS Certificate Manager: CloudFront requires its certificate in us-east-1. I provisioned a public certificate for shortlink.hakt.tech in ACM and validated it with a CNAME in Cloudflare.
  • Rate limiting with AWS WAF: A WAF web ACL on the distribution rate-limits by IP at 1,000 requests per five minutes. That covers both the public GET redirects and signed POSTs that arrive through CloudFront.
  • Origin check (X-Origin-Verify): API Gateway still has a public execute-api endpoint, so a discovered URL could skip CloudFront and WAF. CloudFront injects a secret X-Origin-Verify header on every origin request, and a Lambda authorizer on GET /{token} compares it to the expected value. Direct reads that did not come through CloudFront are rejected. POST / does not use that header. A write still has to carry a valid SigV4 signature.

Compute and database

For the backend, I kept the architecture lean: AWS Lambda for compute and Amazon DynamoDB for storage. A URL shortener is a primary-key lookup, not a relational query, so DynamoDB gives predictable single-digit millisecond reads with nothing to manage. The table is on-demand, with the token as the partition key.

Edge validation and the read Lambda

Public redirect URLs attract bots, typos, and path probing. The read path rejects bad requests as early as it can:

  1. Edge check: On GET, a CloudFront Function tests the path against the 6-character token format (^[A-Za-z0-9]{6}$). /favicon.ico, /wp-admin, a bare /, or a 5-character typo never leave the edge. The function returns a small HTML 404 from a template that is inlined into the function at deploy time. That skips API Gateway, Lambda, and DynamoDB.
  2. Primary-key lookup: If the token shape is valid, GET reaches the read Lambda, which calls GetItem with the token as the partition key.
  3. Redirect or the same HTML page: A hit returns 301 with target_url in Location. A well-formed token that is not in the table returns 404 HTML. The Lambda reads the same error.html the edge function inlined, and fills in a different title and message. The page matches, and the copy says whether the path was nonsense or the link is simply missing.

The write Lambda

POST / takes a JSON body with target_url, mints a short code, and refuses to overwrite an existing one:

  • Base62 keyspace (62^6): Each code is 6 characters from a-z, A-Z, and 0-9, generated with secrets. That is $62^6$ (about 56.8 billion) combinations, which is plenty for an internal service and still a short link.
  • Conditional write: The Lambda puts the item with attribute_not_exists on the token. A collision retries with a new code, up to 3 times. If all three collide, the handler returns 500. There is no distributed counter. At this volume, a random code plus a conditional write is the whole ID scheme.

Conclusion

A URL shortener still earns its place as a system design exercise. On paper it is two endpoints and a key-value store. Wiring it for a real hostname is where the trade-offs show up:

  • Refuse work at the edge: CloudFront, WAF, the origin header, and the token check stop malformed reads before Lambda or DynamoDB run. The HTML for those 404s is one template, used at the edge and in the read Lambda.
  • Match auth to the caller: SigV4 on POST / means there is no public signup and no API key to rotate. GET stays public, and 301 responses are what make repeat reads cheap.
  • Keep the record boring: One DynamoDB table, a 6-character partition key, and a conditional put. Collision handling did not need a ticket service.

Try it

This short link goes through the edge function, the read Lambda, and a 301:

👉 https://shortlink.hakt.tech/yjuM6o

The CDK app behind it is ZackFra/shortlink.