-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
346 lines (313 loc) · 11 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
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import { Input } from "@pulumi/pulumi";
import * as mime from "mime";
import * as path from "path";
import { IWebAclRule } from "./src/types/web-acl-rule";
import { crawlDirectory } from "./src/utils/crawl-directory";
import { createAliasRecord } from "./src/utils/create-alias-record";
import { createWWWAliasRecord } from "./src/utils/create-www-alias-record";
import { getDomainAndSubdomain } from "./src/utils/get-domain-and-subdomain";
import { getDomains } from "./src/utils/get-domains";
import { provisionCloudfrontFunction } from "./src/utils/provisionCloudfrontFunction";
const stackConfig = new pulumi.Config("static-website");
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"),
// If true create an A record for the www subdomain of targetDomain pointing to the generated cloudfront distribution.
// If a certificate was generated it will support this subdomain.
// default: false
includeWwwSubDomain: stackConfig.getBoolean("includeWwwSubDomain") ?? false,
author: stackConfig.get("author") ?? "JonDoe",
org: stackConfig.get("organization") ?? "JonDoe Inc.",
};
const env = pulumi.getStack();
const project = pulumi.getProject();
const tags = {
author: config.author,
stack: env,
repo: project,
deployment: "pulumi",
org: config.org,
};
// Bucket to store the website's static content (html files, css files, images, etc.)
const staticContentBucket = new aws.s3.Bucket(
`${config.targetDomain}-content-bucket`,
{
bucket: config.targetDomain,
tags: tags,
//do not enable website hosting, otherwise private s3 origin will not work
}
);
// Sync the contents of the local source directory with the S3 bucket
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 contentFile = new aws.s3.BucketObject(
relativeFilePath,
{
key: relativeFilePath,
acl: "public-read",
bucket: staticContentBucket,
contentType: mime.getType(filePath) || undefined,
source: new pulumi.asset.FileAsset(filePath),
},
{
parent: staticContentBucket,
}
);
});
//Some services in AWS are global or only available in one region. In both cases we must choose us-east-1 region. E.g. for ACM, WAF
const usEastRegion = new aws.Provider("east", {
profile: aws.config.profile,
region: "us-east-1",
});
console.info(`Cert domain name: ${config.targetDomain}`);
const certificateConfig: aws.acm.CertificateArgs = {
domainName: config.targetDomain,
tags: tags,
validationMethod: "DNS",
subjectAlternativeNames: config.includeWwwSubDomain
? [`www.${config.targetDomain}`]
: [],
};
const certificate = new aws.acm.Certificate("certificate", certificateConfig, {
provider: usEastRegion,
});
const domainParts = getDomainAndSubdomain(config.targetDomain);
const hostedZoneId = aws.route53
.getZone({ name: domainParts.parentDomain }, { async: true })
.then((zone) => zone.zoneId);
const tenMinutes = 60 * 10; // in seconds
/**
* Create a DNS record to prove that we _own_ the domain we're requesting a certificate for.
* See https://docs.aws.amazon.com/acm/latest/userguide/gs-acm-validate-dns.html for more info.
*/
const certificateValidationDomain = new aws.route53.Record(
`${config.targetDomain}-validation`,
{
name: certificate.domainValidationOptions[0].resourceRecordName,
zoneId: hostedZoneId,
type: certificate.domainValidationOptions[0].resourceRecordType,
records: [certificate.domainValidationOptions[0].resourceRecordValue],
ttl: tenMinutes,
}
);
let subdomainCertificateValidationDomain;
if (config.includeWwwSubDomain) {
// ensure we validate the www subdomain as well
subdomainCertificateValidationDomain = new aws.route53.Record(
`${config.targetDomain}-validation2`,
{
name: certificate.domainValidationOptions[1].resourceRecordName,
zoneId: hostedZoneId,
type: certificate.domainValidationOptions[1].resourceRecordType,
records: [certificate.domainValidationOptions[1].resourceRecordValue],
ttl: tenMinutes,
}
);
}
// include the validation record for the www subdomain
const validationRecordFqdns =
subdomainCertificateValidationDomain === undefined
? [certificateValidationDomain.fqdn]
: [
certificateValidationDomain.fqdn,
subdomainCertificateValidationDomain.fqdn,
];
/**
* This is a _special_ resource that waits for ACM to complete validation via the DNS record
* checking for a status of "ISSUED" on the certificate itself. No actual resources are
* created (or updated or deleted).
*
* See https://www.terraform.io/docs/providers/aws/r/acm_certificate_validation.html for slightly more detail
* and https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/resource_aws_acm_certificate_validation.go
* for the actual implementation.
*/
const certificateValidation = new aws.acm.CertificateValidation(
"certificate-validation",
{
certificateArn: certificate.arn,
validationRecordFqdns: validationRecordFqdns,
},
{ provider: usEastRegion }
);
const certificateArn = certificateValidation.certificateArn;
const originAccessControl = new aws.cloudfront.OriginAccessControl(
"origin-access-control",
{
description: "Origin Access Control",
originAccessControlOriginType: "s3",
signingBehavior: "always",
signingProtocol: "sigv4",
name: "oac",
}
);
const distributionAliases = getDomains(config.includeWwwSubDomain, config.targetDomain);
const ruleName = "blockDirectAccessToCloudfront";
const blockDirectAccessToCloudfront: Input<IWebAclRule> = {
action: {
allow: {},
},
name: ruleName,
priority: 100,
statement: {
regexMatchStatement: {
fieldToMatch: {
singleHeader: {
name: "host",
},
},
regexString: `^${config.targetDomain}|^www.${config.targetDomain}`,
textTransformations: [
{
priority: 100,
type: "NONE",
},
],
},
},
visibilityConfig: {
cloudwatchMetricsEnabled: false,
metricName: ruleName,
sampledRequestsEnabled: false,
},
};
const rulesArray = [
blockDirectAccessToCloudfront,
// Add more Rules here if needed
];
const webAcl = new aws.wafv2.WebAcl(
"block-direct-access-to-cloudfront",
{
defaultAction: {
block: {}, // Block all request except what's allowed by the rules in the rulesArray
},
description:
"Web ACL to block direct access to the cfnid.cloudfront.net address",
name: "webAclBlockDirectAccessToCloudfront",
rules: rulesArray,
scope: "CLOUDFRONT",
tags: tags,
visibilityConfig: {
cloudwatchMetricsEnabled: false,
metricName: "webAclBlockDirectAccessToCloudfront",
sampledRequestsEnabled: false,
},
},
{ provider: usEastRegion }
);
const cfFunctions: pulumi.Input<
pulumi.Input<aws.types.input.cloudfront.DistributionDefaultCacheBehaviorFunctionAssociation>[]
> = [];
const cfBuildHeader = provisionCloudfrontFunction();
cfFunctions.push({
eventType: "viewer-request",
functionArn: cfBuildHeader.arn,
});
// distributionArgs configures the CloudFront distribution. Relevant documentation:
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html
// https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html
const distributionArgs: aws.cloudfront.DistributionArgs = {
comment: "Cloudfront Settings for a simple static s3 website",
webAclId: webAcl.arn,
aliases: distributionAliases,
enabled: true,
tags: tags,
//one origin for this distribution: the S3 content bucket
origins: [
{
originId: staticContentBucket.arn,
originAccessControlId: originAccessControl.id,
domainName: staticContentBucket.bucketRegionalDomainName,
},
],
defaultRootObject: "index.html",
// A CloudFront distribution can configure different cache behaviors based on the request path.
// Here we just specify a single, default cache behavior which is just read-only requests to S3.
defaultCacheBehavior: {
targetOriginId: staticContentBucket.arn,
viewerProtocolPolicy: "redirect-to-https",
allowedMethods: ["GET", "HEAD", "OPTIONS"],
cachedMethods: ["GET", "HEAD", "OPTIONS"],
forwardedValues: {
cookies: { forward: "none" },
queryString: false,
},
minTtl: 0,
defaultTtl: tenMinutes,
maxTtl: tenMinutes,
functionAssociations: cfFunctions,
},
// "100" is the cheapest and cover the europe area
priceClass: "PriceClass_100",
customErrorResponses: [
{ errorCode: 404, responseCode: 404, responsePagePath: "/404.html" },
],
restrictions: {
geoRestriction: {
restrictionType: "none",
},
},
viewerCertificate: {
acmCertificateArn: certificateArn, // refer to the ssl cert created earlier
sslSupportMethod: "sni-only",
},
};
const cdn = new aws.cloudfront.Distribution(
`${config.targetDomain}-cdn`,
distributionArgs
);
const bucketPolicy = new aws.s3.BucketPolicy("content-bucket-policy", {
bucket: staticContentBucket.id, // refer to the bucket created earlier
policy: pulumi
.all([cdn.arn, staticContentBucket.arn])
.apply(([cdnArn, bucketArn]) =>
JSON.stringify({
Id: "PolicyForCloudFrontPrivateContent",
Version: "2012-10-17",
Statement: [
{
Sid: "AllowCloudFrontServicePrincipal",
Effect: "Allow",
Principal: {
Service: "cloudfront.amazonaws.com",
}, // Only allow Cloudfront read access.
Action: ["s3:GetObject"],
Resource: [`${bucketArn}/*`],
Condition: {
StringEquals: {
"AWS:SourceArn": cdnArn,
},
},
},
],
})
),
});
// Of course we want to block public access to the bucket
const bucketPublicAccessBlock = new aws.s3.BucketPublicAccessBlock(
"content-bucket-public-access-block",
{
bucket: staticContentBucket.id,
blockPublicAcls: true,
blockPublicPolicy: true,
ignorePublicAcls: true,
restrictPublicBuckets: true,
}
);
const aRecord = createAliasRecord(config.targetDomain, cdn);
if (config.includeWwwSubDomain) {
const cnameRecord = createWWWAliasRecord(config.targetDomain, cdn);
}
// Display all endpoints to terminal at the end of `pulumi up` or `pulumi preview`
export const contentBucketUri = pulumi.interpolate`s3://${staticContentBucket.bucket}`;
export const contentBucketWebsiteEndpoint = staticContentBucket.websiteEndpoint;
export const cloudFrontDomain = cdn.domainName;
export const targetDomainEndpoint = `https://${config.targetDomain}/`;