-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcloudfront-distribution.ts
More file actions
56 lines (52 loc) · 2.19 KB
/
Copy pathcloudfront-distribution.ts
File metadata and controls
56 lines (52 loc) · 2.19 KB
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
import { Fn, Stack } from 'aws-cdk-lib';
import type { HttpApi } from 'aws-cdk-lib/aws-apigatewayv2';
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as cfOrigins from 'aws-cdk-lib/aws-cloudfront-origins';
import * as s3 from 'aws-cdk-lib/aws-s3';
type DistributionSettings = {
indexDotHtml: s3.Bucket;
apiMain: HttpApi;
hasCustomDomain: boolean;
customDomainName?: string;
customDomainCertificateArn?: string;
};
export const instantiateCloudFrontDistribution = (stack: Stack, params: DistributionSettings) => {
const { indexDotHtml, apiMain, hasCustomDomain, customDomainName, customDomainCertificateArn } =
params;
const apiDomain = Fn.select(2, Fn.split('/', apiMain.apiEndpoint));
const cloudfrontDistribution = new cloudfront.Distribution(stack, 'cloudfrontDistribution', {
defaultBehavior: {
origin: new cfOrigins.S3StaticWebsiteOrigin(indexDotHtml, {
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY,
}),
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
compress: true,
originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
},
additionalBehaviors: {
'/api/*': {
origin: new cfOrigins.HttpOrigin(apiDomain, {
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
}),
allowedMethods: cloudfront.AllowedMethods.ALLOW_ALL,
cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
compress: true,
originRequestPolicy: cloudfront.OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER,
},
},
priceClass: cloudfront.PriceClass.PRICE_CLASS_100,
domainNames: hasCustomDomain ? [customDomainName!] : undefined,
certificate: hasCustomDomain
? Certificate.fromCertificateArn(
stack,
'CustomDomainCertificate',
customDomainCertificateArn!,
)
: undefined,
});
return cloudfrontDistribution;
};