AWS CDK Link Shortener Part 1: Project Setup & Basic Infrastructure
Setting up a production-grade link shortener with AWS CDK, DynamoDB, and Lambda. Real architecture decisions, initial setup, and lessons learned from building URL shorteners at scale.
Series Navigation
This is Part 1 of a 5-part series on building a production-grade link shortener:
- Part 1: Project Setup & Basic Infrastructure (You are here)
- Part 2: Core Functionality & API Development
- Part 3: Advanced Features & Security
- Part 4: Production Deployment & Optimization
- Part 5: Scaling & Maintenance
Introduction: Building for Real-World Scale
Last month, during our quarterly planning meeting, the marketing team made an urgent request: "We need branded short links for all our campaigns. Can you build something by next week?" The easy answer would've been to grab a SaaS solution, but when you're handling 5-10 million redirects per month and need custom analytics, building your own starts making sense.
Here's the thing about link shorteners - they seem simple until you hit production. Then you discover all the fun edge cases: redirect loops, malicious URLs, analytics at scale, and my personal favorite - when someone accidentally creates a short link that points to another short link that points back to the first one during a major campaign launch.
Let me walk you through building a production-grade link shortener with AWS CDK that won't wake you up during your vacation.
The Architecture That Survived Black Friday
Before writing any code, I spent a week sketching architectures on napkins (literally - coffee shop napkins are great for system design). Here's what we landed on:
This architecture handles about 2,000 requests per second without breaking a sweat. The key decisions:
- CloudFront for caching - Why hit your Lambda for the same redirect 10,000 times?
- DynamoDB over RDS - Predictable performance at scale, no connection pooling headaches
- Separate Lambda functions - Easier to scale and debug when things go wrong
- DAX for hot paths - Because that one viral link will hammer your database
Setting Up Your CDK Project (The Right Way)
First lesson: don't just run cdk init. Take five minutes to set up your project structure properly. You'll thank yourself later when you're not refactoring everything at 2x the scale.
Your project structure should look like this:
DynamoDB Design: Lessons from High-Volume Production
Here's where most tutorials go wrong - they show you a basic table with id and url. That's cute, but it won't survive production. After three database migrations (each more painful than the last), here's the schema that actually works:
Why this schema? Let me show you with real data:
This design lets you:
- Query all data for a link with one request
- Deduplicate URLs efficiently
- Track individual clicks for analytics
- Support custom slugs without conflicts
- Expire links automatically with TTL
The Lambda That Handles Everything
Here's the create handler that's processed millions of links:
The ID Generator That Won't Fail You
After trying nanoid, shortid, and a bunch of other libraries, here's what actually works in production:
Local Development That Doesn't Suck
Set up local development properly from day one. Trust me, you don't want to deploy to AWS every time you change a console.log:
Run DynamoDB locally:
Deploy Script That Won't Ruin Your Day
Performance Numbers from Production
After running this for 6 months, here are the real numbers:
- Create endpoint: p50: 45ms, p99: 120ms
- Redirect endpoint (cold start): p50: 15ms, p99: 80ms
- Redirect endpoint (warm): p50: 8ms, p99: 25ms
- DynamoDB costs: ~0.25 per million)
- Lambda costs: $12/month (most redirects served from CloudFront)
- CloudFront costs: $85/month (worth every penny for caching)
Lessons Learned the Hard Way
-
Start with on-demand DynamoDB - You don't know your access patterns yet. We switched to provisioned after 3 months and saved 60%.
-
Log everything, retain nothing - We logged every click initially. The CloudWatch bill was... educational. Now we sample 1% and use metrics for the rest.
-
Cache aggressively - That viral link that got 500,000 clicks in an hour? CloudFront saved us from a massive Lambda bill.
-
Validate URLs properly - Someone will try to create a short link to
javascript:alert('xss'). Someone will create redirect loops. Someone will use your service for phishing. Plan for it. -
Rate limiting from day one - We didn't add it initially. Then someone's script created 100,000 links in 10 minutes during a product launch. Fun times.
Next Steps in This Series
Ready to implement the core functionality? In Part 2: Core Functionality & API Development, we'll:
- Build the redirect handler with smart caching strategies
- Implement analytics that won't break the bank
- Add rate limiting and abuse prevention
- Set up monitoring that actually tells you when things are broken
Quick Preview of the Complete Series:
- Part 3: Advanced features including custom domains, QR codes, and bulk operations
- Part 4: Production deployment with blue-green deployments and zero-downtime migrations
- Part 5: Scaling strategies and long-term maintenance patterns
The complete code for this series is on GitHub, including migration scripts and performance tests.
Remember: link shorteners are simple until they're not. Build for scale from the start, but deploy what works today. And always, always validate those URLs.
References
- docs.aws.amazon.com - AWS CDK Developer Guide.
- github.com - AWS CDK source repository and release notes.
- docs.aws.amazon.com - Lambda functions: execution model and scaling.
- docs.aws.amazon.com - Amazon DynamoDB Developer Guide.
- docs.aws.amazon.com - Amazon API Gateway Developer Guide.
- nodejs.org - Node.js official documentation.
- typescriptlang.org - TypeScript Handbook and language reference.
- github.com - TypeScript project wiki (FAQ and design notes).
- docs.aws.amazon.com - AWS Lambda Developer Guide.
- serverless.com - Serverless learning resources (patterns and operations).
- docs.aws.amazon.com - AWS Overview (official whitepaper).
- cloud.google.com - Google Cloud documentation.
AWS CDK Link Shortener: From Zero to Production
A comprehensive 5-part series on building a production-grade link shortener service with AWS CDK, Node.js Lambda, and DynamoDB. Real war stories, performance optimization, and cost management included.