forked from pulumi/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
338 lines (299 loc) · 11.1 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as fs from "fs";
const stackConfig = new pulumi.Config();
const config = {
// websiteDomain is the domain/host to serve content at.
websiteDomain: stackConfig.require("websiteDomain"),
// websiteLogsBucketName is the name of the S3 bucket used for storing access logs.
websiteLogsBucketName: stackConfig.require("websiteLogsBucketName"),
// ACM certificate for the target domain. Must be in the us-east-1 region.
certificateArn: stackConfig.require("certificateArn"),
// redirectDomain is the domain to use for any redirects.
redirectDomain: stackConfig.get("redirectDomain") || undefined,
// originBucketNameOverride is an optional value that can be used to manually pin the
// website to a specific S3 bucket. Values are of the form "bucket-name-123", rather
// than "s3://bucket-name-123".
originBucketNameOverride: stackConfig.get("originBucketNameOverride") || undefined,
// pathToOriginBucketMetadata is the path to the file produced at the end of the
// sync-and-test-bucket script (i.e., scripts/sync-and-test-bucket.sh).
pathToOriginBucketMetadata: stackConfig.require("pathToOriginBucketMetadata"),
};
// originBucketName is the name of the S3 bucket to use as the CloudFront origin for the
// website. This bucket is presumed to exist prior to the Pulumi run; if it doesn't, this
// program will fail.
export let originBucketName: string | undefined;
// If a build metadata file is present and contains valid content, use that for the bucket
// name by default. Will fail if there's a file present that isn't parsable as expected.
if (fs.existsSync(config.pathToOriginBucketMetadata)) {
originBucketName = JSON.parse(fs.readFileSync(config.pathToOriginBucketMetadata).toString()).bucket;
}
// However, if the bucket's been configured manually, use that instead. A manually
// configured bucket means that someone's decided to pin it.
if (config.originBucketNameOverride) {
originBucketName = config.originBucketNameOverride;
}
// If there's still no bucket selected, it's an error.
if (!originBucketName) {
throw new Error("An origin bucket was not specified.");
}
// Fetch the bucket we'll use for the preview or update.
const originBucket = pulumi.output(aws.s3.getBucket({
bucket: originBucketName,
}));
// The origin bucket needs to have the "public-read" ACL so its contents can be read by
// CloudFront and served. But we deny the s3:ListBucket permission to anyone but account
// users to prevent unintended disclosure of the bucket's contents.
const originBucketPolicy = new aws.s3.BucketPolicy("origin-bucket-policy", {
bucket: originBucket.bucket,
policy: originBucket.arn.apply(arn => JSON.stringify({
Version: "2008-10-17",
Statement: [
{
Effect: "Deny",
Principal: "*",
Action: "s3:ListBucket",
Resource: arn,
Condition: {
StringNotEquals: {
"aws:PrincipalAccount": aws.getCallerIdentity().accountId,
},
},
},
],
})),
});
// websiteLogsBucket stores the request logs for incoming requests.
const websiteLogsBucket = new aws.s3.Bucket(
"website-logs-bucket",
{
bucket: config.websiteLogsBucketName,
acl: aws.s3.PrivateAcl,
},
{
protect: true,
},
);
const fiveMinutes = 60 * 5;
const oneHour = fiveMinutes * 12;
const oneWeek = oneHour * 24 * 7;
const oneYear = oneWeek * 52;
const baseCacheBehavior = {
targetOriginId: originBucket.arn,
compress: true,
viewerProtocolPolicy: "redirect-to-https",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
cachedMethods: ["GET", "HEAD", "OPTIONS"],
// S3 doesn't need take any of these values into account when serving content.
forwardedValues: {
cookies: {
forward: "none",
},
queryString: false,
},
minTtl: 0,
defaultTtl: fiveMinutes,
maxTtl: fiveMinutes,
};
// domainAliases is a list of CNAMEs that accompany the CloudFront distribution. Any
// domain name to be used to access the website must be listed here.
const domainAliases = [];
// websiteDomain is the A record for the website bucket associated with the website.
domainAliases.push(config.websiteDomain);
// redirectDomain is the domain to use for fully-qualified 301 redirects.
if (config.redirectDomain) {
domainAliases.push(config.redirectDomain);
}
// distributionArgs configures the CloudFront distribution. Relevant documentation:
// https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html
// https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_cloudfront
const distributionArgs: aws.cloudfront.DistributionArgs = {
enabled: true,
aliases: domainAliases,
// We only specify one origin for this distribution: the S3 content bucket.
origins: [
{
originId: originBucket.arn,
domainName: originBucket.websiteEndpoint,
customOriginConfig: {
// > If your Amazon S3 bucket is configured as a website endpoint, [like we have here] you must specify
// > HTTP Only. Amazon S3 doesn't support HTTPS connections in that configuration.
// tslint:disable-next-line: max-line-length
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesOriginProtocolPolicy
originProtocolPolicy: "http-only",
httpPort: 80,
httpsPort: 443,
originSslProtocols: ["TLSv1.2"],
},
},
],
// Default object to serve when no path is given.
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DefaultRootObject.html
defaultRootObject: "index.html",
defaultCacheBehavior: {
...baseCacheBehavior,
},
orderedCacheBehaviors: [
{
...baseCacheBehavior,
pathPattern: "/css/styles.*.css",
defaultTtl: oneYear,
maxTtl: oneYear,
},
{
...baseCacheBehavior,
pathPattern: "/js/bundle.min.*.js",
defaultTtl: oneYear,
maxTtl: oneYear,
},
{
...baseCacheBehavior,
pathPattern: "/js/search.min.*.js",
defaultTtl: oneYear,
maxTtl: oneYear,
},
{
...baseCacheBehavior,
pathPattern: "/fonts/*",
defaultTtl: oneHour,
maxTtl: oneHour,
},
{
...baseCacheBehavior,
pathPattern: "/icons/*",
defaultTtl: oneHour,
maxTtl: oneHour,
},
{
...baseCacheBehavior,
pathPattern: "/logos/*",
defaultTtl: oneHour,
maxTtl: oneHour,
},
{
...baseCacheBehavior,
pathPattern: "/images/home/*",
defaultTtl: oneHour,
maxTtl: oneHour,
},
// Web-component loaders must not be cached, because the names of the files they
// load will change between builds.
{
...baseCacheBehavior,
pathPattern: "/js/components.js",
defaultTtl: 0,
minTtl: 0,
maxTtl: 0,
},
{
...baseCacheBehavior,
pathPattern: "/js/components/components.js",
defaultTtl: 0,
minTtl: 0,
maxTtl: 0,
},
{
...baseCacheBehavior,
pathPattern: "/js/components/components.esm.js",
defaultTtl: 0,
minTtl: 0,
maxTtl: 0,
},
// Build-metadata files are never cached.
{
...baseCacheBehavior,
pathPattern: "/metadata.json",
defaultTtl: 0,
minTtl: 0,
maxTtl: 0,
},
],
// "All" is the most broad distribution, and also the most expensive.
// "100" is the least broad, and also the least expensive.
priceClass: "PriceClass_All",
// Customize error pages.
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/custom-error-pages.html
customErrorResponses: [
{
errorCode: 404,
responseCode: 404,
errorCachingMinTtl: fiveMinutes,
responsePagePath: "/404.html",
},
],
restrictions: {
geoRestriction: {
restrictionType: "none",
},
},
// CloudFront certs must be in us-east-1, just like API Gateway.
viewerCertificate: {
acmCertificateArn: config.certificateArn,
sslSupportMethod: "sni-only",
minimumProtocolVersion: "TLSv1.2_2018",
},
loggingConfig: {
bucket: websiteLogsBucket.bucketDomainName,
includeCookies: false,
prefix: `${config.websiteDomain}/`,
},
};
// cdn is the CloudFront distribution that serves the content of the website.
const cdn = new aws.cloudfront.Distribution(
"cdn",
distributionArgs,
{
protect: true,
dependsOn: [ websiteLogsBucket ],
},
);
// Split a domain name into its subdomain and parent domain names.
// e.g. "www.example.com" => "www", "example.com".
function getDomainAndSubdomain(domain: string): { subdomain: string, parentDomain: string} {
const parts = domain.split(".");
if (parts.length < 2) {
throw new Error(`No TLD found on ${domain}`);
}
if (parts.length === 2) {
return {
subdomain: "",
parentDomain: domain + ".",
};
}
const subdomain = parts[0];
parts.shift(); // Drop first element.
return {
subdomain,
// Trailing "." to canonicalize.
parentDomain: parts.join(".") + ".",
};
}
// Creates a new Route53 DNS record pointing the domain to the CloudFront distribution.
async function createAliasRecord(
targetDomain: string, distribution: aws.cloudfront.Distribution): Promise<aws.route53.Record> {
const domainParts = getDomainAndSubdomain(targetDomain);
const hostedZone = await aws.route53.getZone({ name: domainParts.parentDomain });
return new aws.route53.Record(
targetDomain,
{
name: domainParts.subdomain,
zoneId: hostedZone.zoneId,
type: "A",
aliases: [
{
name: distribution.domainName,
zoneId: distribution.hostedZoneId,
evaluateTargetHealth: true,
},
],
},
{
protect: true,
});
}
[...new Set(domainAliases)].map(alias => createAliasRecord(alias, cdn));
export const originBucketWebsiteDomain = originBucket.websiteDomain;
export const originBucketWebsiteEndpoint = originBucket.websiteEndpoint;
export const cloudFrontDomain = cdn.domainName;