@spencerbeggs/aws-cdk-domain-redirect
v1.4.2
Published
An AWS CDK construct that makes redirecting HTTP(S) requests to a different domain a breeze.
Downloads
2
Maintainers
Readme
aws-cdk-domain-redirect
Route 53 is one of the cheapest, flexible and most powerful domain registrar and DNS provider around. And yet, there's no straightforward way to just redirect one domain to another like GoDaddy's trusty old domain forwarding feature. AWS Support offers a lackluster solution to the problem, which is to enable website hosting in S3 and redirect using bucket properties. But this prevents your site from using HTTPS — and who doesn't use HTTPS nowadays?
Setting up proper domain fowrarding on AWS involves a mess of CloudFront distributions, Lambda@Edge functions and Route 53 Alias records. Who has time for all that? If you are using AWS Cloud Development Kit to manage your infrastructure, this construct will wire it all together for your in just a couple lines of code.
Note: Lambda@Edge functions must be created in the us-east-1 region, which means that a stack using this constuct does, too.
Basic Usage
By default, this module just needs:
- the name of your hosted zone, e.g.,
source.com
- the ARN of a Certificate Manager certificate that supports your alias, by default
{zoneName}
andwww.{zoneNane}
- the target URL you want to redirect traffic to
import { App, Stack, StackProps } from "@aws-cdk/core";
import { DomainRedirect } from "@spencerbeggs/aws-cdk-domain-redirect";
export class MyStack extends Stack {
public constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
new DomainRedirect(this, "MyRedirects", {
zoneName: "source.com",
cert: "arn:aws:acm:us-east-1:62638843746:certificate/29789573-2b30-469f-cf...",
target: "https://target.com",
});
}
}
You can also pass and array of options objects as the construct props if you want to setup fowrarding for multiple zones and configurations. Passing an array reduces the number of resources in your final template compared to instantiating multiple instances.
Advanced Usage
The configuration above is going to 301 redirect traffic from http(s)://source.com
and http(s)://www.source.com
to https://target.com
. If you want to redirect domains other that the apex and www subdomain, just pass them as an array in the hostnames
property:
new DomainRedirect(this, "MyRedirects", {
zoneName: "source.com",
cert: "arn:aws:acm:us-east-1:62638843746:certificate/29789573-2b30-469f-cf...",
target: "https://target.com",
hostnames: ["foo.source.com", "bar.source.com"],
});
The default configuration will preserve the request pathname and querystring when redirecting to the target. You can selectively control this behavior with the preserve
property:
new DomainRedirect(this, "MyRedirects", {
zoneName: "source.com",
cert: "arn:aws:acm:us-east-1:62638843746:certificate/29789573-2b30-469f-cf...",
target: "https://target.com",
// you can also disable both with preserve: false
preserve: {
path: true,
query: false,
},
});
You can also pass a Certificate construct as the cert property. The zoneName property can also be a HostedZone construct or an IHostedZone, just be aware that you need to look them up by zoneName. Types are bundled with the module.