Package: rayou.cdk-url-shortener
cdk-url-shortenerDeploy a URL shortener with custom domain support in just a few lines of code.cdk-url-shorteneris an AWS CDK L3 construct that will create a URL shortener withcustom domainsupport. The service usesnanoidto generate URL-friendly unique IDs and will retry if an ID collision occurs.Additionally, you can enableDynamoDB streamsto capture changes to items stored in the DynamoDB table.Table of ContentsFeaturesInstallationUsageBasicCustom DomainMultiple Custom DomainsEnable DynamoDB StreamsCreate your first short URLDocumentationConstruct API ReferenceURL Shortener API EndpointsShorten a LinkVisit a shortened URLSupporting this projectLicenseFeatures🚀 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.InstallationTypeScript/JavaScript$npminstall@rayou/cdk-url-shortenerPython$pipinstallrayou.cdk-url-shortener.Net$nugetinstallCDK.URLShortener# See more: https://www.nuget.org/packages/CDK.URLShortener/UsageBasic# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826fromrayou.cdk_url_shortenerimportURLShortenerURLShortener(self,"myURLShortener")Custom Domain# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.aws_route53asroute53importaws_cdk.aws_certificatemanagerasacmfromrayou.cdk_url_shortenerimportURLShortenerzone=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/826importaws_cdk.aws_route53asroute53fromrayou.cdk_url_shortenerimportURLShortenerzone=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/826importaws_cdk.aws_lambdaaslambda_importaws_cdk.aws_dynamodbasdynamodbimportaws_cdk.aws_lambda_event_sourcesaslambda_event_sourcesfromrayou.cdk_url_shortenerimportURLShortenertable=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';\nexports.handler = async (event) => {\nconsole.log('Received event:', JSON.stringify(event, null, 2));\nfor (const record of event.Records) {\nconsole.log(record.eventID);\nconsole.log(record.eventName);\nconsole.log('DynamoDB Record: %j', record.dynamodb);\n}\nconsole.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 URLAfter the deployment, you'll seeApiKeyURLandApiEndpointin 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/k2zxcvbafw6Run this cURL command to create your first short URL, anIDwill be returned in the response.$curlhttps://{API_ENDPOINT}/-XPOST\-H'content-type: application/json'\-H'x-api-key: {API_KEY}'\-d'{"url": "https://github.com/rayou/cdk-url-shortener"}'{"id":"LDkPh"}Visithttps://{API_ENDPOINT}/{ID}then you'll be redirected to the destination URL.$curl-vhttps://{API_ENDPOINT}/{ID}# e.g. https://mydomain.com/LDkPh<HTTP/2301<content-type:text/html;charset=UTF-8
<content-length:309<location:https://github.com/rayou/cdk-url-shortener

<!DOCTYPEhtml><html><head><metacharset="UTF-8"/><metahttp-equiv="refresh"content="0;url=https://github.com/rayou/cdk-url-shortener"/><title>Redirectingtohttps://github.com/rayou/cdk-url-shortener</title></head><body>Redirectingto<ahref="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>DocumentationConstruct API ReferenceSeeAPI.md.URL Shortener API EndpointsShorten a LinkHTTP REQUESTPOST /HEADERSNameValueRequiredcontent-typeapplication/jsonRequiredx-api-keyGet your api keyhereRequiredARGUMENTSParameterTypeRequiredDescriptionurlstringRequiredDestination URLExample Requestcurlhttps://mydomain.com/-XPOST\-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 URLHTTP REQUESTGET /:idExample Requestcurlhttps://mydomain.com/:idResponse (301)<HTTP/2301<content-type:text/html;charset=UTF-8
<content-length:309<location:https://github.com/rayou/cdk-url-shortener

<!DOCTYPEhtml><html><head><metacharset="UTF-8"/><metahttp-equiv="refresh"content="0;url=https://github.com/rayou/cdk-url-shortener"/><title>Redirectingtohttps://github.com/rayou/cdk-url-shortener</title></head><body>Redirectingto<ahref="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>Supporting this projectI'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! ❤️LicenseThis project is distributed under theApache License, Version 2.0.