krtk.rs is a high-performance, serverless URL shortener built with Rust and AWS CDK. Running fully AWS.
β οΈ THIS IS NOT PRODUCTION READYβ οΈ There is no authentication implemented yet. So feel free to explore this project and tinker with it, but it would be unwise to implement this in any sort of production capacity. You have been warned.
This project demonstrates the integration of Rust-based Lambda functions with AWS services like API Gateway, DynamoDB, and CloudFront to create a scalable and responsive web application. The infrastructure is defined as code using AWS CDK, enabling easy deployment and management.
Huge shout out to Luciano and James for making the Crafing Lambda Functions in Rust book which enabled me to build this. π
.
βββ bin
β βββ krtk-rs.ts # CDK app entry point
βββ lambda
β βββ create_link # Lambda function for creating short links
β βββ get_links # Lambda function for retrieving links
β βββ visit_link # Lambda function for handling link visits
β βββ process_analytics # Lambda function for analytics processing
βββ lib
β βββ certificate-stack.ts # Stack for SSL certificate
β βββ krtk-rs-stack.ts # Main infrastructure stack
βββ shared # Shared Rust code
βββ website # Frontend assets
β βββ assets
β β βββ main.js # Frontend JavaScript
β βββ index.html # Main HTML page
βββ test
βββ krtk-rs.test.ts # Tests for the CDK stack (not yet implemented)
- Node.js (v14 or later)
- AWS CLI configured with appropriate credentials
- Rust (latest stable version)
- AWS CDK CLI (v2.177.0 or compatible)
-
Clone the repository:
git clone https://github.com/your-repo/krtk-rs.git cd krtk-rs -
Install dependencies:
npm install -
Build the project:
npm run build
-
Bootstrap your AWS environment (if not already done):
cdk bootstrap -
Deploy the stacks:
cdk deploy --all
This will deploy two stacks:
CertificateStack: Creates an SSL certificate for the domainKrtkRsStack: Deploys the main application infrastructure
After deployment, you can use the URL shortener by:
- Navigating to the deployed website URL (krtk.rs)
- Entering a long URL in the input field
- Clicking "Shorten" to generate a short link
- Using the generated short link to access the original URL
-
User submits a URL to be shortened:
- Frontend JavaScript sends a POST request to
/api/links create_linkLambda function processes the request (tries to get the Website title, and image)- New short link is stored in DynamoDB
- Response with short link ID is sent back to the user
- Frontend JavaScript sends a POST request to
-
User visits a short link:
- Request is routed through CloudFront to API Gateway
visit_linkLambda function looks up the original URL in DynamoDB and returns the original link.- The realtime log is sent from CloudFront to a Kinesis stream.
- The
process_analyticsfunction increments the visit count.
-
Retrieving list of links:
- Frontend JavaScript sends a GET request to
/api/links get_linksLambda function queries DynamoDB for all links- Response with list of links is sent back and displayed on the frontend
- Frontend JavaScript sends a GET request to
[Kinesis] ------------------------+
^ |
| v
[User] -> [CloudFront] -> [API Gateway] -> [Lambda] <-> [DynamoDB]
^ |
| v
+--- [S3 (Static Website)]
The project uses AWS CDK to define and deploy the following resources:
-
Lambda:
createLink: Creates new short linksgetLinks: Retrieves list of linksvisitLink: Handles link visits and redirectsprocessAnalyticsLambda: Handles the CF access logs from kinesis
-
DynamoDB:
linkTable: Stores short link data
-
S3:
hostingBucket: Hosts the static website files
-
CloudFront:
- Distribution for serving the website and API
-
Kinesis:
- Receving realtime acces logs from CloudFront
-
API Gateway:
- HTTP API for handling link operations
-
Route53:
- Hosted zone and records for custom domain
-
ACM:
- SSL certificate for the custom domain
In order to use URL validation, you need to have an Google API Key set up, and the Safe Browsing API enabled in your Google Project. More info can be found here
Here is the stuff that need to be implemented to make this project production-ready:
- Domain name as parameter: Allow changing the TLD from
krtk.rsto something else. - Create a DEV stage: Currently, the PROD stage is very much dev-like. A dedicated DEV stage would be useful for testing.
- Tag all resources: Ensure all resources in the stack are properly tagged for better management.
- Further break down stacks: Consider having separate stacks for Lambda, data, etc., for better modularity.
- Handle blocked websites: Improve handling when scraping website titles and images, especially when blocked by Cloudflare or other protections.
- Implement Auth: Use Amazon Cognito to add authentication.
- Shorter links: Investigate ways to generate shorter URLs (e.g., shorter UUIDs or custom short codes).
- Order links by creation: Figure out how to order retrieved links by date created.
- Pagination: Implement pagination for the front end.
- Add tests for CDK stack: Currently, the tests for the CDK stack are not implemented.
- Improve documentation: Add more detailed documentation for deployment, usage, and troubleshooting.
- Analytics: Add basic analytics to track link usage (e.g., number of clicks, geographic data).
- Custom domains: Allow users to use custom domains for their shortened links.
- Optimize for HTMX: Instead of returning JSON, I should have the ability to return pure HTML and have it just handled by HTMX

