Building and Deploying a URL Shortener function with AWS Lambda

Building and Deploying a URL Shortener function with AWS Lambda

Building and Deploying a URL Shortener function with AWS Lambda

URL shortening is a common practice in web development to create concise and user-friendly links. In this article, we'll explore how to build a serverless URL shortener using AWS Lambda. This setup will allow you to shorten long URLs into more manageable and shareable links.

Prerequisites

Before we start, make sure you have the following:

  1. An AWS account with access to AWS Lambda and API Gateway.
  2. AWS CLI installed and configured.

Steps to Create a URL Shortener

Step 1: Create a Lambda Function

  1. Open the AWS Lambda console.

  2. Click on "Create function."

  3. Choose "Author from scratch."

  4. Enter a name for your function (e.g., URLShortenerFunction).

  5. Select a runtime (e.g., Node.js).

  6. In the function code section, replace the existing code with the following Node.js code:

const { nanoid } = require('nanoid');

exports.handler = async (event) => {
    const originalUrl = event.queryStringParameters.url;
    const shortUrl = generateShortUrl();
    // Some form of persistent logic
    return {
        statusCode: 200,
        body: JSON.stringify({ originalUrl, shortUrl }),
    };
};

function generateShortUrl() {
    // Use nanoid to generate a short URL key
    return 'https://short.url/' + nanoid(7);
}

This code takes a long URL as a query parameter and returns a JSON object containing the original and short URLs.

Note: A design consideration for this solution should include a form of persistence where the original URL and the shortened URL are safely stored.

  1. Under "Basic settings," set the timeout to a reasonable value (e.g., 10 seconds).

  2. Click "Deploy" to create the Lambda function.

Step 2: Create an API Gateway

  1. Open the API Gateway console.
  2. Click on "Create API."
  3. Choose "HTTP API" and click "Build" to create a new API.
  4. In the "Configure routes" section, click "Add integration."
  5. Choose "Lambda function" and select the Lambda function you created.
  6. Click "Create" to create the API.

Step 3: Deploy the API

  1. In the API Gateway console, select your API.
  2. Click on "Deployments" in the left sidebar.
  3. Click "Create" to create a new deployment.
  4. Choose a stage name (e.g., "prod") and click "Deploy."

Step 4: Test the URL Shortener

  1. In the API Gateway console, navigate to the "Invoke URL" section to find your API endpoint.

  2. Use a tool like curl or a web browser to make a GET request to the endpoint with the url query parameter:

curl "<Your API Endpoint>?url=https://www.example.com"

Replace with the actual endpoint URL.

  1. You should receive a JSON response containing the original and short URLs.

Congratulations! You've successfully created a serverless URL shortener using AWS Lambda and API Gateway. This example is a basic implementation, and you can enhance it by adding persistence, customizing the short URL format, and implementing redirection.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!