forked from pulumi/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
392 lines (349 loc) · 13.5 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// 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";
import * as mime from "mime";
import * as path from "path";
const stackConfig = new pulumi.Config();
const config = {
// pathToWebsiteContents is a relative path to the website's contents.
pathToWebsiteContents: stackConfig.require("pathToWebsiteContents"),
// targetDomain is the domain/host to serve content at.
targetDomain: stackConfig.require("targetDomain"),
// alias is an optional domain alias the CDN will support as well.
alias: stackConfig.get("alias") || undefined,
// 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,
};
// redirectDomain is the domain to use when redirecting.
const redirectDomain = config.redirectDomain || config.targetDomain;
// contentBucket stores the static content to be served via the CDN.
const contentBucket = new aws.s3.Bucket(
"contentBucket",
{
bucket: config.targetDomain,
acl: "public-read",
// Have S3 serve its contents as if it were a website. This is how we get the right behavior
// for routes like "foo/", which S3 will automatically translate to "foo/index.html".
website: {
indexDocument: "index.html",
errorDocument: "404.html",
},
},
{
protect: false,
});
// contentBucket needs to have the "public-read" ACL so its contents can be ready by CloudFront and
// served. But we deny the s3:ListBucket permission to prevent unintended disclosure of the bucket's
// contents.
const denyListPolicyState: aws.s3.BucketPolicyArgs = {
bucket: contentBucket.bucket,
policy: contentBucket.arn.apply((arn: string) => JSON.stringify({
Version: "2008-10-17",
Statement: [
{
Effect: "Deny",
Principal: "*",
Action: "s3:ListBucket",
Resource: arn,
},
],
})),
};
const denyListPolicy = new aws.s3.BucketPolicy("deny-list", denyListPolicyState);
// logsBucket stores the request logs for incoming requests.
const logsBucket = new aws.s3.Bucket(
"requestLogs",
{
bucket: `${config.targetDomain}-logs`,
acl: "private",
},
{
protect: true,
});
const fiveMinutes = 60 * 5;
const oneHour = fiveMinutes * 12;
const oneWeek = oneHour * 24 * 7;
const baseCacheBehavior = {
targetOriginId: contentBucket.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,
};
// 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,
// We only specify one origin for this distribution: the S3 content bucket.
origins: [
{
originId: contentBucket.arn,
domainName: contentBucket.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: oneWeek,
maxTtl: oneWeek,
},
{
...baseCacheBehavior,
pathPattern: "/js/bundle.min.*.js",
defaultTtl: oneWeek,
maxTtl: oneWeek,
},
{
...baseCacheBehavior,
pathPattern: "/js/search.min.*.js",
defaultTtl: oneWeek,
maxTtl: oneWeek,
},
{
...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,
},
],
// "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: logsBucket.bucketDomainName,
includeCookies: false,
prefix: `${config.targetDomain}/`,
},
};
// NOTE: Sometimes updating the CloudFront distribution will fail with:
// "PreconditionFailed: The request failed because it didn't meet the preconditions in one or more
// request-header fields."
//
// This is due to https://github.com/pulumi/pulumi/issues/1449:
// Error "CloudFront ETag Out Of Sync" when externally modifying CloudFront resource
const cdn = new aws.cloudfront.Distribution(
"cdn",
distributionArgs,
{
protect: true,
dependsOn: [ contentBucket, logsBucket ],
});
// crawlDirectory recursive crawls the provided directory, applying the provided function
// to every file it contains. Doesn't handle cycles from symlinks.
function crawlDirectory(dir: string, f: (_: string) => void) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = `${dir}/${file}`;
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
crawlDirectory(filePath, f);
}
if (stat.isFile()) {
f(filePath);
}
}
}
// Some files do not get the correct mime/type inferred from the mime package, and we
// need to set our own.
function getMimeType(filePath: string): string | undefined {
// Ensure that latest-version's mime type is always text/plain. Otherwise it
// will end up being set to binary/octet-stream, which is not what we want.
if (path.basename(filePath) === "latest-version") {
return "text/plain";
}
return mime.getType(filePath) || undefined;
}
// Sync the contents of the source directory with the S3 bucket, which will in-turn show up on the CDN.
const webContentsRootPath = path.join(process.cwd(), config.pathToWebsiteContents);
console.log("Syncing contents from local disk at", webContentsRootPath);
crawlDirectory(
webContentsRootPath,
(filePath: string) => {
const relativeFilePath = filePath.replace(webContentsRootPath + "/", "");
const baseArgs = {
acl: "public-read",
key: relativeFilePath,
bucket: contentBucket,
};
// Create a new S3 object for most files, but HTML files that contain a redirect
// just create a stubbed out S3 object with the right metadata so they return a
// 301 redirect (instead of serving the HTML with a meta-redirect). This ensures
// the right HTTP code response is returned for search engines, as well as
// enables better support for URL anchors.
const redirect = getMetaRefreshRedirect(filePath);
if (!redirect) {
const contentFile = new aws.s3.BucketObject(
relativeFilePath,
{
...baseArgs,
source: new pulumi.asset.FileAsset(filePath),
contentType: getMimeType(filePath) || undefined,
},
{
parent: contentBucket,
});
} else {
const s3Redirect = new aws.s3.BucketObject(
relativeFilePath,
{
...baseArgs,
source: new pulumi.asset.FileAsset("/dev/null"), // Empty file.
websiteRedirect: translateRedirect(filePath, redirect),
},
{
parent: contentBucket,
});
}
});
// Returns the redirect URL if filePath is an HTML file that contains a meta refresh tag, otherwise undefined.
function getMetaRefreshRedirect(filePath: string): string | undefined {
// Only .html files contain meta refresh redirects.
if (path.extname(filePath) !== ".html") {
return undefined;
}
// Extract the redirect from the content of the file.
const text = fs.readFileSync(filePath, "utf8");
const regex = /<meta\s+?http-equiv="refresh"\s+?content="0;\s+?url=(.*?)"/gmi;
const match = regex.exec(text);
if (match && match.length === 2) {
const redirect = match[1];
if (!redirect || redirect.length === 0) {
throw new Error(`Meta refresh tag found in "${filePath}" but the redirect URL was empty.`);
}
return redirect;
}
return match && match.length === 2 ? match[1] : undefined;
}
// translateRedirect fixes up the redirect, if needed.
// If the redirect is already prefixed with "https://" or "http://", it is returned unmodified.
// If the redirect starts with "/", it is translated to an `https://${redirectDomain}${redirect}`.
// Otherwise, an Error is thrown.
function translateRedirect(filePath: string, redirect: string): string {
// If the redirect already has the https or http protocol specified, return it.
if (redirect.startsWith("https://") || redirect.startsWith("http://")) {
return redirect;
}
// If the redirect starts with "/", prefix with the redirect domain and return it.
if (redirect.startsWith("/")) {
return `https://${redirectDomain}${redirect}`;
}
// Otherwise, it's not in a format that we expect so throw an error.
throw new Error(`The redirect "${redirect}" in "${filePath}" is not in an expected format.`);
}
// 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,
},
],
});
}
const aRecord = createAliasRecord(config.targetDomain, cdn);
export const contentBucketUri = contentBucket.bucket.apply(b => `s3://${b}`);
export const contentBucketWebsiteDomain = contentBucket.websiteDomain;
export const contentBucketWebsiteEndpoint = contentBucket.websiteEndpoint;
export const cloudFrontDomain = cdn.domainName;