DEV Community

[Mini Project] Serverless URL Shortener Using AWS Lambda, Api Gateway and DynamoDB

Serverless URL Shortener

Before Starting Consider these options too-

  1. For direct steps Go to GitHub - [Mini Project] Serverless URL Shortener
  2. You can Find the Amazon Blog for Build a Serverless, Private URL Shortener - Here
  3. 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:

  1. AWS Account: If you don’t already have an AWS account, you can sign up here.

(below if working with CLI)

  1. 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.
  2. Python 3.x: Our Lambda function will be written in Python. Make sure Python 3.x is installed on your system from python.org.
  3. 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:

  1. Open the AWS Management Console and navigate to DynamoDB.
  2. Click Create Table.
  3. Set the Table name as URLShortener.
  4. Set the Partition key to short_code (Type: String).
    • This will uniquely identify each shortened URL by its short code.
  5. 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:

  1. Navigate to AWS Lambda in the AWS Management Console.
  2. 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).
  3. 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'})
        }
Enter fullscreen mode Exit fullscreen mode
  • 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:

  1. Navigate to API Gateway:

    • Open the AWS Management Console.
    • In the search bar, type API Gateway, and select it from the list.
  2. Create a new API:

    • Click on Create API.
    • Choose HTTP API, and then click Build to begin creating a new API.
  3. 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.
  4. 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.
  5. 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:

  1. Open Postman and create a new request.
  2. Set the request type to POST.
  3. Enter the URL for the shorten route: > https://.execute-api..amazonaws.com/prod/shorten
  4. In the Body tab, select raw and choose JSON as the format.
  5. Add the following JSON payload to the body:
{
  "long_url": "https://www.example.com"
}
Enter fullscreen mode Exit fullscreen mode
  1. Click Send
  • If everything works correctly, you should receive a response with a short_code

Redirect to the Original URL:

  1. 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.

  2. 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:

  1. Open Command Prompt or PowerShell.
  2. 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"}'
Enter fullscreen mode Exit fullscreen mode
  • You should receive a short_code in the response.

Redirect to the Original URL:

  1. 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>"
Enter fullscreen mode Exit fullscreen mode

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:

  1. Delete the DynamoDB Table:

    • Navigate to DynamoDB in the AWS Management Console.
    • Select the URLShortener table, and click Delete.
  2. Delete the Lambda Function:

    • Navigate to Lambda in the AWS Management Console.
    • Select the URLShortenerFunction function, and click Delete.
  3. 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)

Collapse
 
aryan_2309 profile image
ARYAN GUPTA

Can you provide the steps for the same using CLI next time?

Collapse
 
its_tapas_ig profile image
Tapas SInghal

For sure

Collapse
 
lisaross3243 profile image
S umesh

Gread guide for beginners

Collapse
 
shambhavi_chaukiyal21bai profile image
Somya

Good Breakdown for the serverless application, but for testing you can also use Curl command instead of Invoked.

Collapse
 
shambhavi_chaukiyal21bai profile image
Somya

as it supports both Linux and Windows