This repository contains Terraform configurations and AWS Lambda functions to create a simple URL shortener API using AWS API Gateway, AWS Lambda, and DynamoDB. The API allows users to create short URLs and retrieve the original full URLs associated with the short URLs.
The Terraform configurations in this repository create the following AWS resources:
- API Gateway: An API Gateway with HTTP protocol enabled. It has two routes:
- GET /read: Retrieves the original full URL associated with a short URL.
- POST /write: Creates a short URL associated with a full URL.
- AWS Lambda Functions:
- read_lambda: Handles the GET /read route. It retrieves the full URL associated with a short URL from DynamoDB.
- write_lambda: Handles the POST /write route. It stores the short URL and full
- URL mapping in DynamoDB. DynamoDB Table: A DynamoDB table named url-table to store the mappings between short URLs and full URLs.
Before applying the Terraform configurations, make sure you have the following:
- AWS account credentials configured.
- Terraform installed on your local machine.
- Clone this repository:
git clone https://github.com/omerap12/Serverless-url-shortener-api.git
cd Serverless-url-shortener-api
- Initialize Terraform and apply the configurations:
terraform init
terraform apply
- After applying the Terraform configurations, the API Gateway endpoint URLs for read and write operations will be provided as outputs. You can find them in the Terraform output variables.
To add a short URL, use the following curl
command:
curl -X POST -d '{"full_url":"test.com","short_url":"test"}' -H "Content-Type: application/json" -i $api_url_write
To get a Full URL from a short URL, use the following curl
command:
curl -X GET -d '{"short_url":"test"}' -H "Content-Type: application/json" -i $api_url_read
The read_lambda function handles the GET /read route. It takes a short URL as input, retrieves the corresponding full URL from DynamoDB, and returns the full URL in the response.
The write_lambda function handles the POST /write route. It takes a JSON payload containing a short URL and a full URL, stores the mapping in DynamoDB, and returns a success message in the response.
To destroy the resources created by Terraform, run:
terraform destroy