Super simple flask-based URL shortener leveraging AWS S3
For the full serverless solution, refer to Build a Serverless, Private URL Shortener | AWS Compute Blog
I hate API Gateway (you know, it's expensive)
App(flask) -> S3 Redirect(with a optional custom domain) -> Target Site
- S3 Redirect: https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
- S3 Custom Doamin: https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html
- Python 3.6 or above
- S3 Bucket(optionally custom domain)
-
Create S3 bucket, and enable static website hosting: https://docs.aws.amazon.com/AmazonS3/latest/dev/EnableWebsiteHosting.html
-
Optionally, set custom domain for the target bucket: https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html
-
Prepare IAM role or API key with proper permission to the target S3 bucket.
Thanks to
boto3
, all options for providing credentions in boto3 document is good enough.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObjectAcl"
],
"Resource": [
"arn:aws:s3:::your_s3_bucket",
"arn:aws:s3:::your_s3_bucket/*"
]
}
]
}
ENV | Default | Required | Example |
---|---|---|---|
PORT | 5000 | 8080 | |
S3_REGION | us-east-1 | Yes | us-east-1 |
S3_BUCKET | Yes | hello-bucket | |
SHORT_DOMAIN | https://example.com |
Clone and install requirements:
$ pip install -r requirements.txt
Configure env:
$ cp .env.example .env
$ vi .env
Run app:
$ python run.py
$ cp docker-compose-prod.yaml docker-compose.yaml
# fill in ENV values
$ vi docker-compose.yaml
# run
$ docker-compose up -d
Check out helm-git plugin if you don't want to clone entire repository
$ helm install --name my-release -f values.yaml deploy/charts/s3-url-shortener
- freeCodeCamp - How to build a Serverless URL shortener using AWS Lambda and S3
- AWS Compute Blog - Build a Serverless, Private URL Shortener
MIT licensed. See LICENSE for full details.