Before Starting Consider these options too-
- For direct steps Go to GitHub - [Mini Project] Serverless URL Shortener
- You can Find the Amazon Blog for Build a Serverless, Private URL Shortener - Here
- If You Like to Build a serverless URL shortener app without AWS Lambda Go to - Amazon Blog Link
In this tutorial, we will walk through building a Serverless URL Shortener using AWS services like Lambda, API Gateway, and DynamoDB. The goal of this project is to create a highly scalable and serverless system where long URLs are shortened, and users can be redirected to the original URLs using the short codes.
The project is designed to be beginner-friendly and will guide you step-by-step. You’ll also learn about AWS services like Lambda, API Gateway, and DynamoDB, which are commonly used in serverless architectures.
Prerequisites
Before we start, ensure that you have the following prerequisites:
- AWS Account: If you don’t already have an AWS account, you can sign up here.
(below if working with CLI)
- AWS CLI: The AWS Command Line Interface (CLI) will help you interact with AWS services directly from your terminal. Follow the installation instructions for Windows.
- Python 3.x: Our Lambda function will be written in Python. Make sure Python 3.x is installed on your system from python.org.
- Postman: For testing our API, we’ll be using Postman. You can download and install it from here.
Step 1: Set Up DynamoDB Table
The first step in building our serverless URL shortener is to create a DynamoDB table where the shortened URLs and their corresponding long URLs will be stored.
DynamoDB is a fully managed NoSQL database provided by AWS. It allows you to store data in a key-value format, and it scales automatically based on your application's needs.
Steps:
- Open the AWS Management Console and navigate to DynamoDB.
- Click Create Table.
- Set the Table name as
URLShortener
. - Set the Partition key to
short_code
(Type: String).- This will uniquely identify each shortened URL by its short code.
- Click Create.
Step 2: Create the Lambda Function
Next, we need to create an AWS Lambda function that will generate short codes and handle the URL redirection.
What is AWS Lambda?
AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You upload your code, set a trigger, and Lambda takes care of the rest.
Steps:
- Navigate to AWS Lambda in the AWS Management Console.
- Click Create Function.
- Select Author from Scratch.
- Set the Function name as
URLShortenerFunction
. - Set the Runtime to Python 3.x.
- In the Permissions section, create a new role with basic Lambda permissions (AWS will automatically create the role).
- Click Create Function.
Step 3: Add Code to the Lambda Function
After creating the Lambda function, we need to add the Python code that will perform the main logic of the URL shortener. This includes generating a short code for a given long URL and redirecting users when they visit the short code.
Lambda function acts as the business logic handler in this project. It will:
- Receive the original long URL.
- Generate a short code.
- Store the mapping in DynamoDB.
- Redirect the user to the original URL when they use the short code.
Code for Lambda function:
Here’s the Python code that will handle the URL shortening and redirection:
import json
import boto3
import string
import random
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('########')
def generate_short_code():
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(6))
def lambda_handler(event, context):
http_method = event['httpMethod']
if http_method == 'POST':
# Create a short URL
body = json.loads(event['body'])
long_url = body['long_url']
short_code = generate_short_code()
table.put_item(Item={
'short_code': short_code,
'long_url': long_url
})
return {
'statusCode': 200,
'body': json.dumps({'short_code': short_code})
}
elif http_method == 'GET':
# Redirect to the long URL
short_code = event['queryStringParameters']['short_code']
response = table.get_item(Key={'short_code': short_code})
if 'Item' not in response:
return {
'statusCode': 404,
'body': json.dumps({'error': 'Short URL not found'})
}
long_url = response['Item']['long_url']
return {
'statusCode': 301,
'headers': {'Location': long_url}
}
else:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Invalid HTTP method'})
}
- Click Deploy to save the changes. ( You can modify the code as per required including DynamoDB Table Name and test it as well)
Step 4: Set Up API Gateway
What is API Gateway?
API Gateway is a service from AWS that allows developers to create, publish, maintain, monitor, and secure APIs. With API Gateway, you can expose HTTP endpoints that can interact with backend services such as AWS Lambda functions.
Follow these steps to set up API Gateway:
-
Navigate to API Gateway:
- Open the AWS Management Console.
- In the search bar, type API Gateway, and select it from the list.
-
Create a new API:
- Click on Create API.
- Choose HTTP API, and then click Build to begin creating a new API.
-
Define the API Routes:
- POST /shorten: This route will handle the creation of short URLs.
- GET /redirect: This route will redirect users to the original URL based on the short code provided.
-
Integrate Routes with Lambda:
- For each route, select the
URLShortenerFunction
Lambda function that we created in Step 2. - API Gateway will automatically link the function to the routes.
- For each route, select the
-
Deploy the API:
- After setting up the routes, you need to deploy the API.
- Click on Deploy to create a new stage. A stage is an environment for your API (e.g.,
prod
). - Name the stage, such as
prod
. - AWS will provide a URL for the deployed API. This is the URL you will use to interact with your API.
Example API endpoint URL format:
https://.execute-api..amazonaws.com/prod
Explanation of API Gateway's Role:
API Gateway serves as a bridge between the frontend (users) and the backend (Lambda). When a user requests a short URL or tries to access the original URL using the short code, API Gateway routes the requests to the Lambda function, which processes the logic (e.g., generating a short code or retrieving the original URL).
Step 5: Test the URL Shortener
Once you have set up the Lambda function and API Gateway, it’s time to test the URL shortener functionality.
Testing Using Postman (Recommended for Windows)
Postman is a tool that allows you to send HTTP requests and view responses, making it perfect for testing APIs.
Create a Short URL:
- Open Postman and create a new request.
- Set the request type to POST.
- Enter the URL for the
shorten
route: > https://.execute-api..amazonaws.com/prod/shorten - In the Body tab, select raw and choose JSON as the format.
- Add the following JSON payload to the body:
{
"long_url": "https://www.example.com"
}
- Click Send
- If everything works correctly, you should receive a response with a short_code
Redirect to the Original URL:
-
Create a new request in Postman, set it to GET, and enter the following URL:
https://.execute-api..amazonaws.com/prod/redirect?short_code=
Replace with the code you received from the previous step. Click Send
- You should be redirected to the original URL
Testing Using Command Prompt or PowerShell
If you prefer using the Command Prompt or PowerShell, you can test the API using Invoked
.
Create a Short URL:
- Open Command Prompt or PowerShell.
- Use the following command to send a
POST
request:
Invoke-RestMethod -Uri "https://<api-id>.execute-api.<region>.amazonaws.com/prod/shorten" `
-Method Post `
-ContentType "application/json" `
-Body '{"long_url": "https://www.example.com"}'
- You should receive a
short_code
in the response.
Redirect to the Original URL:
- Use the following command to send a
GET
request:
Invoke-RestMethod -Uri "https://<api-id>.execute-api.<region>.amazonaws.com/prod/redirect?short_code=<short_code>"
Replace <short_code>
with the code you received earlier.
- You should be redirected to the original URL.
Step 6: Clean Up
After you’ve finished testing and exploring your serverless URL shortener, you should clean up your AWS resources to avoid ongoing charges.
How to Clean Up:
-
Delete the DynamoDB Table:
- Navigate to DynamoDB in the AWS Management Console.
- Select the
URLShortener
table, and click Delete.
-
Delete the Lambda Function:
- Navigate to Lambda in the AWS Management Console.
- Select the
URLShortenerFunction
function, and click Delete.
-
Delete the API Gateway:
- Navigate to API Gateway.
- Select the API you created, and click Delete.
Conclusion
In this tutorial, we’ve built a simple yet scalable serverless URL shortener using AWS Lambda, API Gateway, and DynamoDB. This solution is lightweight and fully serverless, which means you only pay for the compute power you use and the resources you consume.
We covered the following key components:
- DynamoDB for storing short codes and URLs.
- AWS Lambda for handling the logic of generating short codes and redirecting users.
- API Gateway to expose HTTP routes and connect the Lambda functions to the web.
With this knowledge, you can build other serverless applications using AWS services. Feel free to explore further and enhance this project by adding features like analytics, user authentication, or custom domains.
Contributing
Feel free to contribute to this project by opening issues or submitting pull requests. Here’s how you can help:
- Add new features or improve existing ones.
- Write documentation or tutorials.
- Fix bugs or optimize the code.
Check out the repository: GitHub - [Mini Project] Serverless URL Shortener
Top comments (5)
Can you provide the steps for the same using CLI next time?
For sure
Gread guide for beginners
Good Breakdown for the serverless application, but for testing you can also use Curl command instead of Invoked.
as it supports both Linux and Windows