Previous story was written since two years ago, work is make me so tired, then I just want to relax in free time. Other reason is not found any project or tech I interest. But be a software engineer you need to keep learning the new technology.
Today, I will introduce how to make short url tool with less code in aws cloud.
Motivation
In modern internet, we can find many shorturl web tool via google, like
Reurl: https://reurl.cc/main/tw
ShortUrl: https://www.shorturl.at/
But why them provide the free resource for each one ? Even the simple server could spent the much cost if many clients use in.
- For some sensitive info, google share link, note link (hackmd). We can not ensure the provider whether take your private link to other place.
So this is an reason that why we need personal short url tool.
How we make it
In this section, we’re going to introduce how to make it and provide source code in section end.
Before starting, we have to take some knowledge, it would make us more easily understand how the short url tool work.
Background
The Serverless Framework helps you develop and deploy AWS Lambda functions, along with the AWS infrastructure resources they require (e.g. an API Gateway endpoint).
- Lambda
AWS Lambda is a serverless, event-driven compute service that lets you run code without provisioning or managing infrastructure.
- API Gateway
Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.
Amazon Simple Storage Service (Amazon S3)
Amazon Simple Storage Service (Amazon S3) is an object storage service offering industry-leading scalability, data availability, security, and performance.
Cloudfront
Amazon CloudFront is a content delivery network (CDN) service built for high performance, security, and developer convenience.
Let’s start create !
Overview the architecture diagram.
It looking simple right :). Let’s talk about the architecture.
Architecture
The short url tool have two basic features need to resolve.
1. Store the URL to DB
Client will send the url to lambda through the API endpoint (Route53 -> Cloudfront -> API Gateway), then lambda create the random tag for the url (the tag need to avoid the conflict with other tag as much as possible)
We choose the S3 as the database. Besides cheap, S3 also can be a redirect engine.
So the client’s url will be store as the object in s3, like below picture:
And each object would set the system define in metadata to redirect the website.
2. Get the URL From DB
Lambda will return tag to client, then client can click this tag to go to the url destination.
🥳🥳🥳 Finally we make it 🥳🥳🥳
But we still not discuss about many details of each component, like the setting of the API gateway or cloudfront…
The all setting can found in my source code, you can follow the README document and easily to create your own short url tool via my github repo.