A simple, scalable, and cost-effective URL shortener built with the Serverless Framework on AWS. This service uses API Gateway, Lambda, and DynamoDB to create and redirect short URLs.
- Create Short URLs: Generate a unique, short identifier for any long URL.
- Redirect Short URLs: Automatically redirect users from the short URL to the original long URL.
- Serverless: No servers to manage. The application scales automatically with demand.
- Cost-Effective: Leverages AWS Lambda and DynamoDB, so you only pay for what you use.
- Easy to Deploy: Deploy the entire stack with a single Serverless Framework command.
- Framework: Serverless Framework
- Cloud Provider: AWS (Amazon Web Services)
- Compute: AWS Lambda for running the application logic.
- Database: AWS DynamoDB for storing the URL mappings.
- API: AWS API Gateway to create and manage the REST API endpoints.
- Language: Node.js
- Node.js (v22)
- npm
- Serverless Framework CLI
- An AWS account with configured credentials.
1. Clone the repository:
git clone https://github.com/sureshcstha/url-shortener-aws.git
cd url-shortener-aws
2. Install dependencies:
npm install
3. Deploy the DynamoDB table:
aws cloudformation deploy --template-file infrastructure/dynamoDB-setup.yaml --stack-name url-shortener-db
4. Deploy to AWS:
Run the following command to deploy the Serverless stack (API Gateway, Lambda) to your AWS account.
serverless deploy
After a successful deployment, the Serverless Framework will output the API endpoints in your terminal. This is the base URL for your new shortener service.
1. Shorten a URL
Method: POST
Endpoint: /shorturl
Request Body:
{
"originalUrl": "https://llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.co.uk"
}
Success Response (200):
{
"shortCode": "9jVdQK7",
"shortUrl": "https://tiny.shresthatech.com/9jVdQK7"
}
2. Redirect to orignal URL
Method: GET
Endpoint: /{shortUrl}
- Example:
Visit https://tiny.shresthatech.com/9jVdQK7 in your browser. Your browser will automatically follow the redirect.
3. Look up the original URL
Method: GET
Endpoint: /lookup/{shortUrl}
Success Response (200):
{
"shortCode": "9jVdQK7",
"originalUrl": "https://llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.co.uk"
}
4. Get click statistics
Method: GET
Endpoint: /clickstats/{shortUrl}
Success Response (200):
{
"shortCode": "9jVdQK7",
"clicks": 42,
"lastAccessed": "2025-04-02T20:36:46.792Z"
}
The core configuration is managed in the serverless.yml file.
service:url-shortener- The name of the service.provider: Defines the AWS region, runtime (Node.js version), and environment variables.functions: Defines the Lambda functions, their handlers, and the API Gateway events that trigger them.resources: Defines the AWS resources created by this stack.