profile

Create a URL shortening service using AWS Lamba with Node.js and DynamoDB

To create a URL shortening service using AWS Lambda with Node.js and DynamoDB, follow these steps:

1. Set Up AWS Environment

Make sure you have AWS CLI configured and necessary IAM roles with appropriate permissions for Lambda and DynamoDB.

2. Create DynamoDB Table

Create a DynamoDB table named URLs with the following schema:

3. Create Lambda Functions

You'll need two Lambda functions: one for shortening URLs and another for redirecting.

a. Shorten URL Lambda Function

  1. Set Up the Project Create a new directory and initialize it:
   mkdir shorten-url
   cd shorten-url
   npm init -y
   npm install aws-sdk shortid
  1. Create the Lambda Function Create index.js in the shorten-url directory:
   // index.js
   const AWS = require("aws-sdk");
   const shortid = require("shortid");
   const dynamo = new AWS.DynamoDB.DocumentClient();
   const TABLE_NAME = "URLs";
   exports.handler = async (event) => {
     const { originalUrl } = JSON.parse(event.body);
     const shortUrl = shortid.generate();
     const params = {
       TableName: TABLE_NAME,
       Item: {
         shortUrl: shortUrl,
         originalUrl: originalUrl,
       },
     };
     await dynamo.put(params).promise();
     return {
       statusCode: 200,
       body: JSON.stringify({ shortUrl }),
     };
   };
  1. Deploy the Lambda Function Zip the contents and upload it to AWS Lambda or use the AWS CLI:
   zip -r function.zip .
   aws lambda create-function --function-name shortenUrl --zip-file fileb://function.zip --handler index.handler --runtime nodejs14.x --role arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_LAMBDA_ROLE
  1. Create API Gateway Create a REST API in API Gateway with a POST method that triggers the shortenUrl Lambda function.

b. Redirect URL Lambda Function

  1. Set Up the Project Create a new directory and initialize it:
   mkdir redirect-url
   cd redirect-url
   npm init -y
   npm install aws-sdk
  1. Create the Lambda Function Create index.js in the redirect-url directory:
   // index.js
   const AWS = require("aws-sdk");
   const dynamo = new AWS.DynamoDB.DocumentClient();
   const TABLE_NAME = "URLs";
   exports.handler = async (event) => {
     const { shortUrl } = event.pathParameters;
     const params = {
       TableName: TABLE_NAME,
       Key: {
         shortUrl: shortUrl,
       },
     };
     const result = await dynamo.get(params).promise();
     if (result.Item) {
       return {
         statusCode: 301,
         headers: {
           Location: result.Item.originalUrl,
         },
       };
     } else {
       return {
         statusCode: 404,
         body: JSON.stringify({ error: "URL not found" }),
       };
     }
   };
  1. Deploy the Lambda Function Zip the contents and upload it to AWS Lambda or use the AWS CLI:
   zip -r function.zip .
   aws lambda create-function --function-name redirectUrl --zip-file fileb://function.zip --handler index.handler --runtime nodejs14.x --role arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_LAMBDA_ROLE
  1. Create API Gateway Create a REST API in API Gateway with a GET method that triggers the redirectUrl Lambda function and includes {shortUrl} as a path parameter.

4. Configure API Gateway

Ensure that both the POST endpoint for shortening URLs and the GET endpoint for redirection are properly configured in API Gateway.

5. Test Your Service

Final Thoughts

This setup leverages AWS Lambda for serverless execution and DynamoDB for persistent storage, ensuring scalability and cost-effectiveness. The API Gateway facilitates the interaction between clients and your Lambda functions.