-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathwebsite-redirect.ts
109 lines (99 loc) · 4.13 KB
/
website-redirect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as crypto from 'crypto';
import { DnsValidatedCertificate, ICertificate } from '@aws-cdk/aws-certificatemanager';
import { CloudFrontWebDistribution, OriginProtocolPolicy, PriceClass, ViewerProtocolPolicy } from '@aws-cdk/aws-cloudfront';
import { ARecord, AaaaRecord, IHostedZone, RecordTarget } from '@aws-cdk/aws-route53';
import { CloudFrontTarget } from '@aws-cdk/aws-route53-targets';
import { Bucket, RedirectProtocol } from '@aws-cdk/aws-s3';
import { Construct, RemovalPolicy, Stack, Token } from '@aws-cdk/core';
/**
* Properties to configure an HTTPS Redirect
*/
export interface HttpsRedirectProps {
/**
* Hosted zone of the domain which will be used to create alias record(s) from
* domain names in the hosted zone to the target domain. The hosted zone must
* contain entries for the domain name(s) supplied through `recordNames` that
* will redirect to the target domain.
*
* Domain names in the hosted zone can include a specific domain (example.com)
* and its subdomains (acme.example.com, zenith.example.com).
*
*/
readonly zone: IHostedZone;
/**
* The redirect target fully qualified domain name (FQDN). An alias record
* will be created that points to your CloudFront distribution. Root domain
* or sub-domain can be supplied.
*/
readonly targetDomain: string;
/**
* The domain names that will redirect to `targetDomain`
*
* @default - the domain name of the hosted zone
*/
readonly recordNames?: string[];
/**
* The AWS Certificate Manager (ACM) certificate that will be associated with
* the CloudFront distribution that will be created. If provided, the certificate must be
* stored in us-east-1 (N. Virginia)
*
* @default - A new certificate is created in us-east-1 (N. Virginia)
*/
readonly certificate?: ICertificate;
}
/**
* Allows creating a domainA -> domainB redirect using CloudFront and S3.
* You can specify multiple domains to be redirected.
*/
export class HttpsRedirect extends Construct {
constructor(scope: Construct, id: string, props: HttpsRedirectProps) {
super(scope, id);
const domainNames = props.recordNames ?? [props.zone.zoneName];
if (props.certificate) {
const certificateRegion = Stack.of(this).parseArn(props.certificate.certificateArn).region;
if (!Token.isUnresolved(certificateRegion) && certificateRegion !== 'us-east-1') {
throw new Error(`The certificate must be in the us-east-1 region and the certificate you provided is in ${certificateRegion}.`);
}
}
const redirectCertArn = props.certificate ? props.certificate.certificateArn : new DnsValidatedCertificate(this, 'RedirectCertificate', {
domainName: domainNames[0],
subjectAlternativeNames: domainNames,
hostedZone: props.zone,
region: 'us-east-1',
}).certificateArn;
const redirectBucket = new Bucket(this, 'RedirectBucket', {
websiteRedirect: {
hostName: props.targetDomain,
protocol: RedirectProtocol.HTTPS,
},
removalPolicy: RemovalPolicy.DESTROY,
});
const redirectDist = new CloudFrontWebDistribution(this, 'RedirectDistribution', {
defaultRootObject: '',
originConfigs: [{
behaviors: [{ isDefaultBehavior: true }],
customOriginSource: {
domainName: redirectBucket.bucketWebsiteDomainName,
originProtocolPolicy: OriginProtocolPolicy.HTTP_ONLY,
},
}],
aliasConfiguration: {
acmCertRef: redirectCertArn,
names: domainNames,
},
comment: `Redirect to ${props.targetDomain} from ${domainNames.join(', ')}`,
priceClass: PriceClass.PRICE_CLASS_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
});
domainNames.forEach((domainName) => {
const hash = crypto.createHash('md5').update(domainName).digest('hex').substr(0, 6);
const aliasProps = {
recordName: domainName,
zone: props.zone,
target: RecordTarget.fromAlias(new CloudFrontTarget(redirectDist)),
};
new ARecord(this, `RedirectAliasRecord${hash}`, aliasProps);
new AaaaRecord(this, `RedirectAliasRecordSix${hash}`, aliasProps);
});
}
}