This project provides a URL shortener service using AWS Lambda functions and API Gateway. It consists of two Lambda functions: one for shortening URLs and another for redirecting users from shortened URLs to the original URLs. The service uses an S3 bucket to store the mapping between shortened URLs and original URLs (I know that this is not the best way to store this information, but it is a simple way to demonstrate how to use S3 with Lambda functions).
This Lambda function is responsible for generating a shortened URL and storing the original URL in an S3 bucket.
- Name:
url-shorter - Description: A Lambda function to shorten a URL and store the original URL in S3.
- Dependencies:
aws-sdk: AWS SDK for JavaScriptdotenv: Module to load environment variables from a.envfileuuid: Library to generate unique identifiers
The Lambda function uses the following environment variables, which should be defined in a .env file:
BUCKET_NAME=my-s3-bucket-name
GATEWAY_URL=https://your-api-id.execute-api.your-region.amazonaws.com/your-stageThis Lambda function is responsible for redirecting users from the shortened URL to the original URL by looking up the mapping stored in the S3 bucket.
- Name:
url-redirect - Description: A Lambda function to get the original URL from a shortened URL and redirect to it.
- Dependencies:
aws-sdk: AWS SDK for JavaScriptdotenv: Module to load environment variables from a.envfile
The Lambda function uses the following environment variables, which should be defined in a .env file:
BUCKET_NAME=my-s3-bucket-name- Deploy the Lambda functions using your preferred deployment method (e.g., AWS SAM, Serverless Framework, or manually via the AWS Console).
- Configure the API Gateway to route requests to the appropriate Lambda functions.
- Define the environment variables in the
.envfiles for each Lambda function. - Test the URL shortener service by sending HTTP requests to the API Gateway endpoints.
POST gateway-url/create
{
"url": "https://www.example.com"
}GET gateway-url/{shortened-code}
Replace {shortened-code} with the code returned from the URL shortener Lambda function.