Deploy an URL shortener API.
Uses a DynamoDB table to increment a counter. The value of the counter is base62 encoded and then a zero-byte object with redirection is stored in S3.
Define a UrlShortener
:
import { Stack, StackProps } from 'aws-cdk-lib';
import * as route53 from 'aws-cdk-lib/aws-route53';
import * as cloudstructs from 'cloudstructs';
import { Construct } from 'constructs';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// The hosted zone for the domain of the short urls
const hostedZone = new route53.HostedZone(this, 'HostedZone', { zoneName: 'short.com' });
new cloudstructs.UrlShortener(this, 'UrlShortener', { hostedZone });
}
}
The deployed API expects the following JSON body:
{
"url": "https://www.mylongurl.com/very/long/path"
}
and replies with:
{
"url": "https://www.mylongurl.com/very/long/path",
"shortUrl": "https://short.com/trBkV"
}
By default, the API is public. It can be made private by specifying
the apiGatewayEndpoint
prop.
An authorizer can be added to the API by specifying the apiGatewayAuthorizer
prop.
Enable CORS by passing the corsAllowOrigins
prop.