cdk-url-shortener
Deploy a URL shortener with custom domain support in just a few lines of code.
cdk-url-shortener is an AWS CDK L3 construct that will create a URL shortener with custom domain support. The service uses nanoid to generate URL-friendly unique IDs and will retry if an ID collision occurs.
Additionally, you can enable DynamoDB streams to capture changes to items stored in the DynamoDB table.
Table of Contents
Features
- 🚀 Easy to Start - One-liner code to have your own URL shortener.
- 🏢 Custom Domain - Bring your custom domain name that fits your brand.
- 📡 DynamoDB Streams - Capture table activity with DynamoDB Streams.
Installation
TypeScript/JavaScript
$ npm install @rayou/cdk-url-shortener
Python
$ pip install rayou.cdk-url-shortener
.Net
$ nuget install CDK.URLShortener
# See more: https://www.nuget.org/packages/CDK.URLShortener/
Usage
Basic
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from rayou.cdk_url_shortener import URLShortener
URLShortener(self, "myURLShortener")
Custom Domain
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_route53 as route53
import aws_cdk.aws_certificatemanager as acm
from rayou.cdk_url_shortener import URLShortener
zone = route53.HostedZone.from_lookup(self, "HostedZone",
domain_name="mydomain.com"
)
# Optional, a DNS validated certificate will be created if not provided.
certificate = acm.Certificate.from_certificate_arn(self, "Certificate", "arn:aws:acm:region:123456789012:certificate/12345678-1234-1234-1234-123456789012")
URLShortener(self, "myURLShortener").add_domain_name(
domain_name="foo.mydomain.com",
zone=zone,
certificate=certificate
)
Multiple Custom Domains
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_route53 as route53
from rayou.cdk_url_shortener import URLShortener
zone = route53.HostedZone.from_lookup(self, "HostedZone",
domain_name="mydomain.com"
)
URLShortener(self, "myURLShortener").add_domain_name(
domain_name="foo.mydomain.com",
zone=zone
).add_domain_name(
domain_name="bar.mydomain.com",
zone=zone
)
⚠️ Please note that although we have added two custom domains, they are pointed to the same URL shortener instance sharing the same DynamoDB table, if you need both domains run independently, create a new URL shortener instance.
Enable DynamoDB Streams
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_lambda as lambda_
import aws_cdk.aws_dynamodb as dynamodb
import aws_cdk.aws_lambda_event_sources as lambda_event_sources
from rayou.cdk_url_shortener import URLShortener
table = dynamodb.Table(self, "Table",
partition_key=Attribute(
name="id",
type=dynamodb.AttributeType.STRING
),
stream=dynamodb.StreamViewType.NEW_AND_OLD_IMAGES
)
URLShortener(self, "myURLShortener",
dynamo_table=table
)
stream_handler_code = "'use strict';\n exports.handler = async (event) => {\n console.log('Received event:', JSON.stringify(event, null, 2));\n for (const record of event.Records) {\n console.log(record.eventID);\n console.log(record.eventName);\n console.log('DynamoDB Record: %j', record.dynamodb);\n }\n console.log(`Successfully processed ${event.Records.length} records.`);\n };"
lambda_fn = lambda_.Function(self, "myStreamHandler",
runtime=lambda_.Runtime.NODEJS_12_X,
handler="index.handler",
code=lambda_.Code.from_inline(stream_handler_code)
)
lambda_fn.add_event_source(
lambda_event_sources.DynamoEventSource(table,
starting_position=lambda_.StartingPosition.LATEST
))
Create your first short URL
-
After the deployment, you'll see
ApiKeyURLandApiEndpointin CDK Outputs, visitApiKeyURLto get your API key.Outputs: stack.CustomDomainApiEndpointcc4157 = https://mydomain.com stack.myURLShortenerApiEndpoint47185311 = https://yrzxcvbafk.execute-api.us-west-2.amazonaws.com/prod/ stack.ApiKeyURL = https://console.aws.amazon.com/apigateway/home?#/api-keys/k2zxcvbafw6
-
Run this cURL command to create your first short URL, an
IDwill be returned in the response.$ curl https://{API_ENDPOINT} / -X POST \ -H 'content-type: application/json' \ -H 'x-api-key: {API_KEY}' \ -d '{ "url": "https://github.com/rayou/cdk-url-shortener" }' {"id":"LDkPh"}
-
Visit
https://{API_ENDPOINT}/{ID}then you'll be redirected to the destination URL.$ curl -v https://{API_ENDPOINT}/{ID} # e.g. https://mydomain.com/LDkPh < HTTP/2 301 < content-type: text/html; charset=UTF-8 < content-length: 309 < location: https://github.com/rayou/cdk-url-shortener <!DOCTYPE html><html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=https://github.com/rayou/cdk-url-shortener" /><title>Redirecting to https://github.com/rayou/cdk-url-shortener</title></head><body>Redirecting to <a href="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>
Documentation
Construct API Reference
See API.md.
URL Shortener API Endpoints
Shorten a Link
HTTP REQUEST
POST /
HEADERS
| Name | Value | Required |
|---|---|---|
content-type |
application/json |
Required |
x-api-key |
Get your api key here | Required |
ARGUMENTS
| Parameter | Type | Required | Description |
|---|---|---|---|
url |
string | Required | Destination URL |
Example Request
curl https://mydomain.com /
-X POST \
-H 'content-type: application/json' \
-H 'x-api-key: v3rYsEcuRekey' \
-d '{
"url": "https://github.com/rayou/cdk-url-shortener"
}'
Response (201)
{
"id": "LDkPh"
}
Visit a shortened URL
HTTP REQUEST
GET /:id
Example Request
curl https://mydomain.com/:id
Response (301)
< HTTP/2 301
< content-type: text/html; charset=UTF-8
< content-length: 309
< location: https://github.com/rayou/cdk-url-shortener
<!DOCTYPE html><html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=https://github.com/rayou/cdk-url-shortener" /><title>Redirecting to https://github.com/rayou/cdk-url-shortener</title></head><body>Redirecting to <a href="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>
Supporting this project
I'm working on this project in my free time, if you like my project, or found it helpful and would like to support me, you can buy me a coffee, any contributions are much appreciated! ❤️
License
This project is distributed under the Apache License, Version 2.0.
Metadata
Release files for rayou.cdk-url-shortener 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rayou.cdk-url-shortener-0.1.2.tar.gz | 42.4 kB | Without provenance | Details for rayou.cdk-url-shortener-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rayou.cdk_url_shortener-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for rayou.cdk_url_shortener-0.1.2-py3-none-any.whl |
Total release size: 83.0 kB
Release files / rayou.cdk-url-shortener-0.1.2.tar.gz
| Download URL | rayou.cdk-url-shortener-0.1.2.tar.gz |
|---|---|
| Size | 42.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
957d83aec71c1acba90f4ab0af031265bba5225b37610c1e6acd8091e5e71573
|
|
BLAKE2b-256 checksum How to use checksums |
20285e6703f5b4018b54c321270a7970ca485fb9d291b3b39b8cacd4e84e773d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
|
Release files / rayou.cdk_url_shortener-0.1.2-py3-none-any.whl
| Download URL | rayou.cdk_url_shortener-0.1.2-py3-none-any.whl |
|---|---|
| Size | 40.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
80ffd3eafa57bed97fcdd36db517172dc477f44424796c335f0d2bec2131949d
|
|
BLAKE2b-256 checksum How to use checksums |
5c2eb0fbbcd55721a9a5a961330032bdcb7592453945b3ad7c3965b6dd26e977
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8
|
Security for rayou.cdk-url-shortener 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.