-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
295 lines (258 loc) · 11.3 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
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 tenMinutes = 60 * 10;
// 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);
}
}
}
// 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}`);
}
// No subdomain, e.g. awesome-website.com.
if (parts.length === 2) {
return { subdomain: "", parentDomain: domain };
}
const subdomain = parts[0];
parts.shift(); // Drop first element.
return {
subdomain,
// Trailing "." to canonicalize domain.
parentDomain: parts.join(".") + ".",
};
}
// Creates a new Route53 DNS record pointing the domain to the CloudFront distribution.
function createCnameRecord(
targetDomain: string,
distribution: aws.cloudfront.Distribution,
opts: pulumi.ResourceOptions): aws.route53.Record {
const domainParts = getDomainAndSubdomain(targetDomain);
const hostedZoneId = aws.route53.getZone({ name: domainParts.parentDomain }).then(zone => zone.zoneId);
return new aws.route53.Record(
targetDomain,
{
name: domainParts.subdomain,
zoneId: hostedZoneId,
type: "CNAME",
aliases: [
{
name: distribution.domainName,
zoneId: distribution.hostedZoneId,
evaluateTargetHealth: true,
},
],
},
opts);
}
/**
* Arguments to StaticWebsite concerning the website's contents.
*/
export interface ContentArgs {
/**
* Path to the content files to serve relative to the CWD of the Pulumi program.
*/
pathToContent: string;
/**
* Path to the resource to serve if the CDN fails to locate the intended
* resource.
*/
custom404Path?: string;
}
/**
* Arguments to StaticWebsite optionally specifying how a domain should be attached.
*/
export interface DomainArgs {
// targetDomain is Route53 hosted domain to create the domain on. If it is
// a subdomain, ("www.example.com"), the A record "www" will be created. If
// it is the root domain ("example.com"), the A record "" will be created
// instead.
targetDomain: string;
// acmCertificateArn is the ARN to an ACM certificate, matching the target
// domain. Must be in the us-east-1 region; this is a requirement imposed
// by CloudFront.
acmCertificateArn: string;
}
/**
* Static website using Amazon S3, CloudFront, and Route53.
*/
export class StaticWebsite extends pulumi.ComponentResource {
// contentBucket is the S3 bucket that the website's contents will be
// stored in. Note that the contents of the S3 bucket will be publicly
// visible, just like the resulting website.
readonly contentBucket: aws.s3.Bucket;
// logsBucket is an S3 bucket that will contain the CDN's request logs.
// Will be private, and can later be queried using AWS Athena.
readonly logsBucket: aws.s3.Bucket;
// cdn is the actual CloudFront distribution which speeds up delivery
// by caching content in edge nodes across the world.
readonly cdn: aws.cloudfront.Distribution;
// cnameRecord is the CNAME record created on the target domain which
// points to the CDN. If DomainArgs is not specified, will be null.
readonly cnameRecord?: aws.route53.Record;
/**
* Creates a new static website hosted on AWS.
* @param name The _unique_ name of the resource.
* @param contentArgs The arguments to configure the content being served.
* @param domainArgs The arguments to configure the domain and DNS settings.
* @param opts A bag of options that control this resource's behavior.
*/
constructor(name: string, contentArgs: ContentArgs, domainArgs?: DomainArgs, opts?: pulumi.ResourceOptions) {
const inputs: pulumi.Inputs = {
options: opts,
};
super("pulumi-contrib:components:StaticWebsite", name, inputs, opts);
// Default resource options for this component's child resources.
const defaultResourceOptions: pulumi.ResourceOptions = { parent: this };
// Create/populate the S3 bucket storing the website's content.
this.contentBucket = new aws.s3.Bucket(
`${name}-content`,
{
acl: "public-read",
// Configure S3 to serve bucket contents as a website. This way
// S3 will automatically convert requests for "foo/" to
// "foo/index.html".
website: {
indexDocument: "index.html",
errorDocument: "404.html",
},
}, defaultResourceOptions);
// Sync the contents of the source directory with the S3 bucket, which
// will in-turn be served by S3, and then cached via CloudFront.
const webContentsRootPath = path.join(process.cwd(), contentArgs.pathToContent);
if (!fs.existsSync(webContentsRootPath)) {
throw `Error: website contents path '${webContentsRootPath} does not exist.`;
}
if (!fs.statSync(webContentsRootPath).isDirectory()) {
throw `Error: website contents path '${webContentsRootPath} is not a directory.`;
}
crawlDirectory(
webContentsRootPath,
(filePath: string) => {
const relativeFilePath = filePath.replace(webContentsRootPath + "/", "");
// Create each file as a separate BucketObject. This is slower
// than manually copying files to the bucket, e.g. via the CLI,
// but allows for Pulumi to track resource status. So a
// subsequent update will delete unreferenced files.
const contentFile = new aws.s3.BucketObject(
relativeFilePath,
{
key: relativeFilePath,
acl: "public-read",
bucket: this.contentBucket,
contentType: mime.getType(filePath) || undefined,
source: new pulumi.asset.FileAsset(filePath),
},
{
parent: this.contentBucket,
});
});
// Create the logs bucket to store CloudFront request logs.
this.logsBucket = new aws.s3.Bucket(
`${name}-logs`,
{
acl: "private",
},
defaultResourceOptions);
// Optionally specify the ACM certificate to use for HTTPS requests.
let certificateInfo = {};
if (domainArgs) {
certificateInfo = {
acmCertificateArn: domainArgs.acmCertificateArn,
sslSupportMethod: "sni-only",
};
}
// Optionally specify custom error handlers.
const customErrors = [];
if (contentArgs.custom404Path) {
// Fail with a friendly error message than "InvalidArgument: The parameter ResponsePagePath is invalid."
if (!contentArgs.custom404Path.startsWith("/")) {
throw new Error("custom404Path must be prefixed with a slash.");
}
customErrors.push({
errorCode: 404,
responseCode: 404,
responsePagePath: contentArgs.custom404Path,
});
}
// distributionArgs configures the CloudFront distribution. Relevant documentation:
// https://docs.aws.amazon.com/AmazonCloudFront/latest/
// https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html
const distributionArgs: aws.cloudfront.DistributionArgs = {
enabled: true,
aliases: domainArgs ? [ domainArgs.targetDomain ] : [],
// We only specify one origin for this distribution, the S3 content bucket.
origins: [
{
originId: this.contentBucket.arn,
domainName: this.contentBucket.websiteEndpoint,
customOriginConfig: {
// Amazon S3 doesn't support HTTPS connections when using an S3 bucket as a website endpoint.
// tslint:disable-next-line
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesOriginProtocolPolicy
originProtocolPolicy: "http-only",
httpPort: 80,
httpsPort: 443,
originSslProtocols: ["TLSv1.2"],
},
},
],
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: this.contentBucket.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,
},
// "All" is the most broad distribution, and also the most expensive.
// "100" is the least broad, and also the least expensive.
priceClass: "PriceClass_100",
// You can customize error responses. When CloudFront recieves an error from the origin (S3) it will
// instead return some other resource instead.
customErrorResponses: customErrors,
restrictions: {
geoRestriction: {
restrictionType: "none",
},
},
// HTTPS certificate information if applicable.
viewerCertificate: certificateInfo,
loggingConfig: {
bucket: this.logsBucket.bucketDomainName,
includeCookies: false,
prefix: `${name}/`,
},
};
this.cdn = new aws.cloudfront.Distribution(`${name}-cdn`, distributionArgs, defaultResourceOptions);
// Create/Update DNS record if desired.
this.cnameRecord = undefined;
if (domainArgs) {
this.cnameRecord = createCnameRecord(domainArgs.targetDomain, this.cdn, defaultResourceOptions);
}
}
}