DEV Community

Cover image for Building a Serverless URL Shortener with AWS
Samir Khanal
Samir Khanal

Posted on

Building a Serverless URL Shortener with AWS

Hey there, fellow developers and cloud enthusiasts! If you're like me, you've probably wondered how those sleek, short links on social media work behind the scenes. Today, I'm excited to dive into a project I built: a serverless URL shortener that combines DevOps automation, real-time analytics, and scalable architecture. It's not just a fun tool; it's a blueprint for modern, event-driven applications on AWS. Let's break it down step by step, from concept to deployment.

Overview

In the fast-paced world of web development, sharing links efficiently is crucial. Long URLs can be cumbersome, especially on mobile devices or character-limited platforms. Enter our serverless URL shortener: a robust system that takes any URL and generates a short, memorable code (e.g., https://mydomain.com/ABC123) that redirects users seamlessly.

Built entirely on AWS, this project showcases serverless best practices. It handles URL shortening, click tracking, analytics processing via queues, and even integrates with CI/CD pipelines for automated deployments. I designed this to demonstrate how serverless can simplify complex workflows while keeping costs low and scalability high. Think of it as a digital post office: you drop off a package (long URL), get a tracking number (short code), and the system handles delivery (redirects) with full visibility.

Goals

The primary goals were straightforward yet ambitious:

  • Simplicity and Scalability: Create a URL shortener that scales automatically without server management.
  • Analytics Insights: Track clicks and events in real-time for user behavior analysis.
  • DevOps Integration: Automate deployments via CI/CD, with notifications for workflow triggers.
  • Community Value: Provide an open-source example for AWS builders, highlighting services like Lambda, Step Functions, and SQS.

By the end, I wanted a production-ready app that could handle thousands of requests with minimal overhead—perfect for startups, marketers, or anyone needing link management.

Architecture

At its core, this is a fully serverless architecture leveraging AWS's event-driven model. Here's a high-level view:

GitLab / API Trigger
        ↓
   API Gateway
        ↓
Lambda (url-shortener)
     ↓        ↓        ↓
DynamoDB   SQS Queue   Step Functions (url_workflow)
     ↓        ↓
Analytics Lambda   Slack Notifications
Enter fullscreen mode Exit fullscreen mode

  • API Gateway: Acts as the entry point for shortening and redirecting URLs.
  • Lambda Functions: Handle logic—url-shortener for CRUD operations, analytics for processing queued events.
  • DynamoDB: NoSQL database storing URLs, short codes, and click data.
  • SQS: Decouples analytics processing, ensuring reliability during traffic spikes.
  • Step Functions: Orchestrates workflows for notifications (e.g., Slack alerts on URL creation).
  • CloudWatch: Monitors logs and metrics; integrates with GitLab for CI/CD.

Development Process

Development followed an iterative, Terraform-driven approach. I started with Terraform for infrastructure as code, defining resources in main.tf. Here's a snippet of the API Gateway setup:

resource "aws_api_gateway_rest_api" "url_shortener_api" {
  name        = "${var.project_name}-url-api"
  description = "URL Shortener API"
}

resource "aws_api_gateway_method" "shorten_method" {
  rest_api_id   = aws_api_gateway_rest_api.url_shortener_api.id
  resource_id   = aws_api_gateway_resource.shorten_resource.id
  http_method   = "POST"
  authorization = "NONE"
}
Enter fullscreen mode Exit fullscreen mode

Next, I wrote Lambda functions in Node.js. The url-shortener Lambda handles shortening and redirects, using DynamoDB for storage. Check out this key function:

async function shortenUrl(event) {
  const body = JSON.parse(event.body || '{}');
  const { url } = body;
  const shortCode = generateShortCode();

  await dynamo.put({
    TableName: TABLE_NAME,
    Item: { shortCode, originalUrl: url, clicks: 0 }
  }).promise();

  // Trigger Step Functions for notifications
  await stepfunctions.startExecution({
    stateMachineArn: STATE_MACHINE_ARN,
    input: JSON.stringify({ action: 'url_created', shortCode, originalUrl: url })
  }).promise();

  return { statusCode: 201, body: JSON.stringify({ shortCode, shortUrl }) };
}
Enter fullscreen mode Exit fullscreen mode

Testing involved local simulations with AWS SAM or Docker, followed by deployments via GitLab pipelines. Each push triggered the DevOps workflow, sending Slack notifications and queuing messages.

Key Features

  • URL Shortening & Redirects: Generate codes and handle redirects with click tracking.
  • Real-Time Analytics: Queue events to SQS; process via Lambda for insights.
  • Slack Integration: Instant notifications via Step Functions (e.g., "New URL created!").
  • GitLab CI/CD: Automated builds and deployments with webhook triggers.
  • Monitoring: CloudWatch dashboards for metrics—[Screenshot 2: CloudWatch graph showing Lambda invocations].

These features make it user-friendly: Developers can integrate via APIs, while stakeholders get alerts and reports.

Challenges

Serverless isn't without hurdles. Cold starts in Lambda caused initial latency—mitigated with provisioned concurrency. SQS message visibility timeouts need tuning to avoid duplicates during high loads. Debugging Step Functions required tracing executions in the console. Security was key: I used IAM roles and environment variables to protect sensitive data like Slack webhooks.

Lessons Learned

This project reinforced serverless principles: Embrace events over servers. Use queues for decoupling to build resilient systems. Community tools like Terraform and GitLab sped up development. Sharing code snippets and docs helped others—open-source wins!

Future Plans

Next, I'll add A/B testing for short codes, integrate with Kinesis for advanced analytics, and explore multi-region deployments. If the community engages, I might add a UI dashboard. Contributions welcome—check the GitHub repo!

Conclusion

Building this URL shortener was a journey in serverless excellence, proving AWS can power scalable apps with ease. Whether you're a developer experimenting with Lambda or a stakeholder seeking efficient tools, this project shows the power of event-driven architecture. What serverless project are you tackling? Drop your thoughts in the comments—let's build together!

Github link: https://github.com/Shawmeer/aws-serverless-url-shortener

Check more blogs at: https://khanalsamir.com

Top comments (0)