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.
-
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
-
Go: Lambda function runtime
-
AWS Lambda: compute logic
-
DynamoDB: persistent URL storage and click tracking
-
API Gateway: HTTP interface
-
Terraform: deploys all AWS resources
Prerequisites
-
Go installed (>= 1.19)
-
Terraform installed (>= 1.0)
-
AWS CLI configured (aws configure)
git clone https://github.com/your-username/url-shortener-serverless.git
cd url-shortener-serverless
cd lambda
GOOS=linux GOARCH=amd64 go build -o main main.go
zip main.zip main
cd ..
terraform init
terraform apply
Confirm with yes when prompted.
// Request
POST /shorten
Content-Type: application/json
{
"url": "https://example.com"
}
// Response
{
"ShortUrl": "http://localhost:4566/abc123",
"Hash": "abc123"
}
// Request
GET /{hash}
Will HTTP 302 Redirect to the original URL.
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
This project is open-source and available under the MIT License.