Skip to content

A lightweight, serverless URL shortener built with Go, AWS Lambda, DynamoDB, and API Gateway. Features in-memory caching for the 10 most accessed URLs for ultra-fast redirects. Infrastructure is fully managed via Terraform.

License

Notifications You must be signed in to change notification settings

claudiocaldeirao/url-shortener

Repository files navigation

URL Shortener Serverless API (Go + AWS)

A lightweight, fully serverless URL shortener built in Go, powered by AWS Lambda, DynamoDB, and API Gateway. Includes in-memory caching for the top 10 most accessed URLs to ensure ultra-fast redirects. Infrastructure managed via Terraform for easy deployment.

✨ Features

  • Shorten any URL with a simple POST request

  • Fast redirects using DynamoDB and Lambda memory cache

  • Tracks click counts per URL

  • Serverless architecture (zero server maintenance)

  • Infrastructure-as-Code with Terraform

πŸ—οΈ Architecture

  • Go: Lambda function runtime

  • AWS Lambda: compute logic

  • DynamoDB: persistent URL storage and click tracking

  • API Gateway: HTTP interface

  • Terraform: deploys all AWS resources

πŸš€ Getting Started

Prerequisites

  • Go installed (>= 1.19)

  • Terraform installed (>= 1.0)

  • AWS CLI configured (aws configure)

1. Clone the repository

git clone https://github.com/your-username/url-shortener-serverless.git
cd url-shortener-serverless

2. Build the Lambda function

cd lambda
GOOS=linux GOARCH=amd64 go build -o main main.go
zip main.zip main
cd ..

3. Deploy with Terraform

terraform init
terraform apply

Confirm with yes when prompted.

πŸ“¬ API Usage

1. Shorten a URL

// Request

POST /shorten
Content-Type: application/json

{
  "url": "https://example.com"
}

// Response
{
  "ShortUrl": "http://localhost:4566/abc123",
  "Hash": "abc123"
}

2. Redirect to the original URL

// Request

GET /{hash}

Will HTTP 302 Redirect to the original URL.

πŸ“¦ Project Structure

url-shortener/
β”œβ”€β”€ app/             # Go source code
β”‚   β”œβ”€β”€ main.go
β”‚   β”œβ”€β”€ db
β”‚   β”‚   └── db.go
β”‚   β”œβ”€β”€ model
β”‚   β”‚   └── ShortUrl.go
β”‚   β”œβ”€β”€ config
β”‚   β”‚   └── config.go
β”‚   └── utils
β”‚       └── utils.go # hash method to generate short url
β”œβ”€β”€ main.tf          # Terraform AWS resources
β”œβ”€β”€ variables.tf     # Terraform variables
└── README.md

πŸ›‘ License

This project is open-source and available under the MIT License.

About

A lightweight, serverless URL shortener built with Go, AWS Lambda, DynamoDB, and API Gateway. Features in-memory caching for the 10 most accessed URLs for ultra-fast redirects. Infrastructure is fully managed via Terraform.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published