From a9e5f15070d0578cd417fd7444d34ccafb29cf3b Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 27 Aug 2021 12:35:54 +0930 Subject: [PATCH] Add a feature flag to enable the error config --- lib/static-hosting.ts | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/static-hosting.ts b/lib/static-hosting.ts index dcc5006..6845015 100644 --- a/lib/static-hosting.ts +++ b/lib/static-hosting.ts @@ -1,6 +1,6 @@ import { Construct, CfnOutput, RemovalPolicy, StackProps, Stack} from '@aws-cdk/core'; import { Bucket, BucketEncryption, BlockPublicAccess } from '@aws-cdk/aws-s3'; -import { OriginAccessIdentity, CloudFrontWebDistribution, PriceClass, ViewerProtocolPolicy, SecurityPolicyProtocol, SSLMethod, Behavior, SourceConfiguration } from '@aws-cdk/aws-cloudfront'; +import { OriginAccessIdentity, CloudFrontWebDistribution, PriceClass, ViewerProtocolPolicy, SecurityPolicyProtocol, SSLMethod, Behavior, SourceConfiguration, CloudFrontWebDistributionProps } from '@aws-cdk/aws-cloudfront'; import { HostedZone, RecordTarget, ARecord } from '@aws-cdk/aws-route53'; import { CloudFrontTarget } from '@aws-cdk/aws-route53-targets'; import { User, Group, Policy, PolicyStatement, Effect } from '@aws-cdk/aws-iam'; @@ -24,6 +24,7 @@ export interface StaticHostingProps { * Optional set of behaviors to override the default behvior defined in this construct */ behaviors?: Array; + enableErrorConfig: boolean; } export class StaticHosting extends Construct { @@ -124,24 +125,34 @@ export class StaticHosting extends Construct { originConfigs = originConfigs.concat(props.customOriginConfigs); } - const distribution = new CloudFrontWebDistribution(this, 'BucketCdn', { - aliasConfiguration: { - acmCertRef: props.certificateArn, - names: distributionCnames, - securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2018, - sslMethod: SSLMethod.SNI, - }, - originConfigs, - errorConfigurations: [{ + + let distributionProps: CloudFrontWebDistributionProps = { + aliasConfiguration: { + acmCertRef: props.certificateArn, + names: distributionCnames, + securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2018, + sslMethod: SSLMethod.SNI, + }, + originConfigs, + priceClass: PriceClass.PRICE_CLASS_ALL, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + loggingConfig: loggingConfig, + }; + + if (props.enableErrorConfig) { + distributionProps = { + ...distributionProps, ...{ + errorConfigurations: [{ errorCode: 404, errorCachingMinTtl: 0, responseCode: 200, responsePagePath: '/index.html', - }], - priceClass: PriceClass.PRICE_CLASS_ALL, - viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, - loggingConfig: loggingConfig, - }); + }] + } + } + } + + const distribution = new CloudFrontWebDistribution(this, 'BucketCdn', distributionProps) if(publisherGroup) { const cloudFrontInvalidationPolicyStatement = new PolicyStatement({