-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
129 lines (109 loc) · 3.36 KB
/
stack.js
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
const { Stack, App, RemovalPolicy } = require('aws-cdk-lib');
const {
HostedZone,
ARecord,
RecordTarget
} = require('aws-cdk-lib/aws-route53');
const { Bucket, BlockPublicAccess } = require('aws-cdk-lib/aws-s3');
const { StringParameter } = require('aws-cdk-lib/aws-ssm');
const { Certificate } = require('aws-cdk-lib/aws-certificatemanager');
const {
OriginAccessIdentity,
CloudFrontWebDistribution,
PriceClass,
ViewerProtocolPolicy,
ViewerCertificate,
SSLMethod,
SecurityPolicyProtocol
} = require('aws-cdk-lib/aws-cloudfront');
const { CloudFrontTarget } = require('aws-cdk-lib/aws-route53-targets');
const { BucketDeployment, Source } = require('aws-cdk-lib/aws-s3-deployment');
class PublicWebStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
const rootFile = 'index.html';
const appName = this.node.tryGetContext('app');
const version = this.node.tryGetContext('version');
const domain = this.node.tryGetContext('domain');
const configRootKey = `/${appName}/${version}`;
const zone = HostedZone.fromLookup(this, 'Zone', {
domainName: domain
});
const bucket = new Bucket(this, 'Bucket', {
bucketName: `${this.region}.${domain}`,
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
removalPolicy: RemovalPolicy.RETAIN
});
const certificateArn = StringParameter.valueForStringParameter(
this,
`${configRootKey}/clientCertificateArn`
);
const certificate = Certificate.fromCertificateArn(
this,
'Certificate',
certificateArn
);
const accessIdentity = new OriginAccessIdentity(this, 'AccessIdentity', {
comment: `${appName}-public-web-identity`
});
const distribution = new CloudFrontWebDistribution(this, 'Distribution', {
priceClass: PriceClass.PRICE_CLASS_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
viewerCertificate: ViewerCertificate.fromAcmCertificate(certificate, {
aliases: [domain],
sslMethod: SSLMethod.SNI,
securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2021
}),
originConfigs: [
{
s3OriginSource: {
s3BucketSource: bucket,
originAccessIdentity: accessIdentity
},
behaviors: [
{
isDefaultBehavior: true,
forwardedValues: {
queryString: true
}
}
]
}
],
errorConfigurations: [
{
errorCode: 403,
responseCode: 200,
responsePagePath: `/${rootFile}`
},
{
errorCode: 404,
responseCode: 200,
responsePagePath: `/${rootFile}`
}
]
});
new ARecord(this, 'Mount', {
recordName: domain,
target: RecordTarget.fromAlias(new CloudFrontTarget(distribution)),
zone
});
new BucketDeployment(this, 'Deployment', {
distribution: distribution,
destinationBucket: bucket,
distributionPaths: [`/${rootFile}`],
sources: [Source.asset('build')]
});
}
}
const app = new App();
new PublicWebStack(app, 'PublicWebStack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION
},
stackName: `${app.node.tryGetContext(
'app'
)}-public-web-${app.node.tryGetContext('version')}`
});
app.synth();