-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdk-stack.js
70 lines (52 loc) · 1.86 KB
/
cdk-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
#!/usr/bin/env node
const cdk = require("aws-cdk-lib");
const { Bucket } = require("aws-cdk-lib/aws-s3");
const cloudfront = require("aws-cdk-lib/aws-cloudfront");
const origins = require("aws-cdk-lib/aws-cloudfront-origins");
const s3deploy = require("aws-cdk-lib/aws-s3-deployment");
const app = new cdk.App();
const envStageName = app.node.tryGetContext("env");
if (!envStageName) {
throw new Error(
`run with parameters:
--context env=ENVIRONMENT_NAME (i.e. dev, test, live, etc.)`
);
}
const stackId = "GGS-Frontend-" + envStageName;
// S3 has a global name restriction per region.
// If someone grabs the default bucket name, change it here.
const BUCKET_NAME = stackId;
const DISTRIBUTION_NAME = stackId + "-Distribution";
const DEPLOY_NAME = stackId + "-DeployWithInvalidation";
class CdkFrontendStack extends cdk.Stack {
constructor(scope) {
super(scope, stackId);
// S3 bucket to host web client files
const bucket = new Bucket(this, BUCKET_NAME, {});
// CloudFront distribution for website
const distribution = new cloudfront.Distribution(
this,
DISTRIBUTION_NAME,
{
defaultBehavior: {
origin: new origins.S3Origin(bucket),
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
viewerProtocolPolicy:
cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
defaultRootObject: "index.html",
}
);
new s3deploy.BucketDeployment(this, DEPLOY_NAME, {
sources: [s3deploy.Source.asset("./build")],
destinationBucket: bucket,
distribution,
});
new cdk.CfnOutput(this, stackId + " URL", {
value: "https://" + distribution.domainName,
description: "External URL for " + stackId + " website",
});
}
}
const frontendStack = new CdkFrontendStack(app);
cdk.Tags.of(frontendStack).add("DeployEnvironment", envStageName);