-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticWebsiteStack.ts
95 lines (81 loc) · 3.07 KB
/
StaticWebsiteStack.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
import cdk = require('@aws-cdk/core');
import {
CloudFrontWebDistribution,
CloudFrontWebDistributionProps,
OriginAccessIdentity,
} from '@aws-cdk/aws-cloudfront'
import { Bucket } from '@aws-cdk/aws-s3';
import { BucketDeployment, Source } from '@aws-cdk/aws-s3-deployment';
import * as iam from '@aws-cdk/aws-iam';
export class StaticWebsiteStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, staticWebsiteConfig: IStaticWebsiteProps) {
super(scope, id, undefined);
const resourcePrefix = staticWebsiteConfig.resourcePrefix;
const deploymentVersion = staticWebsiteConfig.deploymentVersion;
const originPath = deploymentVersion.replace(/\./g, '_');
const sourceBucket = new Bucket(this, `S3BucketForWebsite`, {
websiteIndexDocument: staticWebsiteConfig.indexDocument || 'index.html',
bucketName: `${resourcePrefix}-website`,
});
new BucketDeployment(this, 'DeployWebsite', {
sources: [Source.asset(staticWebsiteConfig.websiteDistPath)],
destinationBucket: sourceBucket,
destinationKeyPrefix: originPath,
});
const cloudFrontOia = new OriginAccessIdentity(this, 'OIA', {
comment: `${resourcePrefix}_oia`,
});
// See AWS-CDK Issue: https://github.com/aws/aws-cdk/issues/941
let cloudFrontDistProps: CloudFrontWebDistributionProps;
if (staticWebsiteConfig.certificateArn) {
cloudFrontDistProps = {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket,
originAccessIdentity: cloudFrontOia,
},
behaviors: [{ isDefaultBehavior: true }],
originPath: `/${originPath}`,
},
],
aliasConfiguration: {
acmCertRef: staticWebsiteConfig.certificateArn,
names: staticWebsiteConfig.domainNames || [],
},
};
} else {
cloudFrontDistProps = {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket,
originAccessIdentity: cloudFrontOia,
},
behaviors: [{ isDefaultBehavior: true }],
originPath: `/${originPath}`,
},
],
};
}
new CloudFrontWebDistribution(this, `${resourcePrefix}-cloudfront`, cloudFrontDistProps);
const policyStatement = new iam.PolicyStatement();
policyStatement.addActions('s3:GetBucket*');
policyStatement.addActions('s3:GetObject*');
policyStatement.addActions('s3:List*');
policyStatement.addResources(sourceBucket.bucketArn);
policyStatement.addResources(`${sourceBucket.bucketArn}/*`);
cloudFrontOia.grantPrincipal.addToPolicy(policyStatement);
policyStatement.addPrincipals(cloudFrontOia.grantPrincipal);
//policyStatement.addCanonicalUserPrincipal(cloudFrontOia.grantPrincipal.addToPolicy());
sourceBucket.addToResourcePolicy(policyStatement);
}
}
export interface IStaticWebsiteProps {
websiteDistPath: string;
deploymentVersion: string
certificateArn?: string;
domainNames?: Array<string>;
resourcePrefix: string;
indexDocument?: string;
}