This repo will contain everything relevant to setting up your URL Shortener service in AWS.
- IAM
- API Gateway
- Lambda
- DynamoDB
- S3
Purpose: Allow Lambda to access DynamoDB Steps:
- Go to IAM > Roles > Create role.
- Select
AWS serviceas trusted entity type. - For
Use case, selectLambda - Attach policies:
AmazonDynamoDBFullAccesspolicyAWSLambdaBasicExecutionRolepolicy
- Name it
lambda-url-shortener-role
Purpose: Store mappings of short code ↔ original URL Steps:
- Go to DynamoDB > Create table
- Table name:
UrlShortenerTable - Partition key:
short_key(String) - Leave other settings default and create table
Purpose: Handle short URL creation and redirection logic Steps:
- Go to Lambda > Create function
- Name it
urlShortenerHandler - Runtime: Python 3.12 (or latest)
- Attach the IAM role from Step 1
- Navigate to the
ConfigurationTab- Click
Edit - Click
Add environment variable - Add environment variables:
- For key fill in
TABLE_NAME, For Value fill inUrlShortenerTable - For key fill in
API_GATEWAY_URL, For Value fill inTHIS WILL BE POPULATED AFTER API GATEWAY IS CREATED
- For key fill in
- Click
- Paste the code for GET & POST URL logic from
lambda_function.py
- Create a REST API
- Go to API Gateway in the AWS Console.
- Click Create API → Choose REST API (not HTTP) → Build.
- Set:
- API name: e.g.,
url-shortener-api - Endpoint Type:
Regionalis fine. - IP address type: Select
IPv4
- API name: e.g.,
- Click
Create API
- Create Resource
/shorten- Click your
shortener-apiAPI - Click
Create resource- Leave
Proxy resourcetoggled OFF (leave it as is) - Type
shortenfor theResource name - Leave
CORS (Cross Origin Resource Sharing)disabled (leave it as is)
- Leave
- Click
Create resource
- Click your
- Create Method: POST /shorten
- Select
/shorten - Click Actions → Create Method → Method type: POST
- Integration type: Lambda Function
- Check "Use Lambda Proxy Integration"
- Lambda function: your Lambda name
- Click Save and confirm permissions
- Select
- Click
/shortenmethod and selectEnable CORSwithinResource details- Allow OPTIONS,POST for the
Access-Control-Allow-Methods - Click
Save
- Allow OPTIONS,POST for the
- Click
/and then click Create Resource:/{short_code}- Click Actions → Create Resource
- Resource name:
{short_code} - Click Create Resource
- Create Method:
GET/{short_code}- Select
/{short_code} - Click Actions → Create Method → GET
- Integration type: Lambda Function
- Check
Use Lambda Proxy Integration - Lambda function: your Lambda name
- Click
Save
- Select
- Click
/{short_code}method and selectEnable CORSwithinResource details- Allow OPTIONS,GET for the
Access-Control-Allow-Methods - Click
Save
- Allow OPTIONS,GET for the
- Click
Deploy API- For
Stage, click*New stage* - For
Stage name, type inv0 - Click
Deploy- If successful, you'll see
Successfully created deployment for url-shortener-api2. This deployment is active for v0.in green banner.
- If successful, you'll see
- For
- Once created and deployed, you will see something like
Resources - url-shortener-api (8tz35msz86),8tz35msz86will be your unique API Gateway ID.- Fill this API Gateway ID into your Lambda's environment variable and replace the value for API_GATEWAY_URL with the API Gateway ID (In my case, it is
8tz35msz86) - Similarly, in line 43 of index.html make sure to use the correct ID. Ex:
const API_BASE = "https://8tz35msz86.execute-api.us-west-1.amazonaws.com/v0";
- Fill this API Gateway ID into your Lambda's environment variable and replace the value for API_GATEWAY_URL with the API Gateway ID (In my case, it is
Purpose: Host the frontend (HTML, CSS, JS) Steps:
- Create a new bucket (e.g., url-shortener123)
- Enable static website hosting
- Within the
Propertiestab - Scroll all the way down to the
Static website hostingand clickEdit - Enable for
Static website hosting - For the Index document, type
index.hmtl - Keep the rest the same.
- Within the
- Upload index.html and any other files
- Set permissions:
- Make sure to turn off
Block all public access - For
Bucket policypaste the below snipped of Policy.
- Make sure to turn off
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::url-shortener1234/*"
}
]
}
- Use the static site endpoint (e.g., http://bucket-name.s3-website-region.amazonaws.com)