This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
frontend-stack.ts
145 lines (127 loc) · 4.94 KB
/
frontend-stack.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
import * as cloudfront from '@aws-cdk/aws-cloudfront';
import * as s3 from '@aws-cdk/aws-s3';
import * as s3deploy from '@aws-cdk/aws-s3-deployment';
import * as core from '@aws-cdk/core';
import * as targets from "@aws-cdk/aws-route53-targets";
import * as bootstrapKit from "aws-bootstrap-kit";
import {
DnsValidatedCertificate,
CertificateValidation,
} from "@aws-cdk/aws-certificatemanager";
import * as route53 from "@aws-cdk/aws-route53";
export interface FrontendStackProps extends core.StackProps {
stage?: string;
rootDomain?: string;
serviceName: string;
}
/**
* Static site infrastructure, which deploys site content to an S3 bucket and expose it through Cloudfront.
*/
export class FrontendStack extends core.NestedStack {
public readonly distribution: cloudfront.CloudFrontWebDistribution;
public readonly frontendUrl: string;
public readonly siteBucket: s3.Bucket;
constructor(parent: core.Construct, name: string, props: FrontendStackProps) {
super(parent, name);
core.Tags.of(this).add(
"ServiceName",
props.serviceName
);
const uiBuildFolder = `${__dirname}/../../../ui/build`;
// Content bucket
this.siteBucket = new s3.Bucket(this, 'SiteBucket', {
websiteIndexDocument: "index.html",
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
});
const landingPageOAI = new cloudfront.OriginAccessIdentity(this, "SiteOAI");
// If DNS info is available, creating all the records to get a proper dns name
if (props.rootDomain) {
const stage = props.stage??'dev';
this.frontendUrl = `${props.serviceName}.${stage}.${props.rootDomain}`;
const delegatedHostedZone = new bootstrapKit.CrossAccountDNSDelegator(
this,
"subzoneDelegation",
{
zoneName: this.frontendUrl,
}
);
const certificate = new DnsValidatedCertificate(
this,
"Certificate",
{
hostedZone: delegatedHostedZone.hostedZone,
domainName: this.frontendUrl,
region: "us-east-1",
validation: CertificateValidation.fromDns(
delegatedHostedZone.hostedZone
),
}
);
const viewerCertificate = cloudfront.ViewerCertificate.fromAcmCertificate(
certificate,
{
aliases: [this.frontendUrl],
}
);
this.distribution = new cloudfront.CloudFrontWebDistribution(
this,
"SiteDistribution",
{
originConfigs: [
{
s3OriginSource: {
s3BucketSource: this.siteBucket,
originAccessIdentity: landingPageOAI,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
viewerCertificate: viewerCertificate
? viewerCertificate
: undefined,
}
);
new route53.ARecord(this, "Alias", {
zone: delegatedHostedZone.hostedZone,
recordName: this.frontendUrl,
target: route53.RecordTarget.fromAlias(
new targets.CloudFrontTarget(this.distribution)
),
});
} else {
// CloudFront distribution without specific dns records
this.distribution = new cloudfront.CloudFrontWebDistribution(
this,
'SiteDistribution',
{
originConfigs: [
{
s3OriginSource: {
s3BucketSource: this.siteBucket,
originAccessIdentity: landingPageOAI,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
}
);
this.frontendUrl = this.distribution.distributionDomainName;
}
// Deploy site contents to S3 bucket
new s3deploy.BucketDeployment(this, 'DeployWithInvalidation', {
sources: [s3deploy.Source.asset(uiBuildFolder)],
destinationBucket: this.siteBucket,
distribution: this.distribution,
distributionPaths: ['/*'],
});
}
}