Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure DefaultTTL for CloudFront #1691

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion deployment/lib/artifacts-public-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CanonicalUserPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
import { Architecture, Runtime } from '@aws-cdk/aws-lambda';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { IBucket } from '@aws-cdk/aws-s3';
import { CfnOutput } from '@aws-cdk/core';
import { CfnOutput, Duration } from '@aws-cdk/core';
import { BuildArtifactStack } from './build-artifact-stack';

export class ArtifactsPublicAccess {
Expand Down Expand Up @@ -47,6 +47,10 @@ export class ArtifactsPublicAccess {
eventType: LambdaEdgeEventType.VIEWER_REQUEST,
lambdaFunction: urlRewriter.currentVersion,
}],
// set ttl to 5mins. Note that changing minTtl or maxTtl may have co-related impact on actual values being used by CloudFront between defaultTtl, maxTtl and minTtl.
// Make sure that you understand https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesObjectCaching
// before changing minTtl or maxTtl.
defaultTtl: Duration.seconds(300)
},
],
},
Expand Down
22 changes: 19 additions & 3 deletions deployment/test/build-artifact-stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
expect, countResources, haveOutput, not,
} from '@aws-cdk/assert';
import { countResources, expect, haveOutput, haveResourceLike, not } from '@aws-cdk/assert';
import { App } from '@aws-cdk/core';
import { BuildArtifactStack } from '../lib/build-artifact-stack';

Expand All @@ -14,7 +12,16 @@ test('Fresh BuildArtifact Stack', () => {
expect(stack).to(countResources('AWS::S3::Bucket', 1));
expect(stack).to(countResources('AWS::IAM::Role', 4));
expect(stack).to(countResources('AWS::CloudFront::CloudFrontOriginAccessIdentity', 1));

expect(stack).to(countResources('AWS::CloudFront::Distribution', 1));
expect(stack).to(haveResourceLike('AWS::CloudFront::Distribution', {
DistributionConfig: {
DefaultCacheBehavior: {
DefaultTTL: 300
}
}
}));

expect(stack).to(countResources('AWS::Lambda::Function', 1));
expect(stack).to(haveOutput({ outputName: 'BuildDistributionDomainName' }));
expect(stack).to(not(haveOutput({ outputName: 'OriginAccessIdentityS3Identifier' })));
Expand All @@ -30,7 +37,16 @@ test('Existing BuildArtifact Stack', () => {
expect(stack).to(countResources('AWS::S3::Bucket', 0));
expect(stack).to(countResources('AWS::IAM::Role', 1));
expect(stack).to(countResources('AWS::CloudFront::CloudFrontOriginAccessIdentity', 1));

expect(stack).to(countResources('AWS::CloudFront::Distribution', 1));
expect(stack).to(haveResourceLike('AWS::CloudFront::Distribution', {
DistributionConfig: {
DefaultCacheBehavior: {
DefaultTTL: 300
}
}
}));

expect(stack).to(countResources('AWS::Lambda::Function', 1));
expect(stack).to(haveOutput({ outputName: 'BuildDistributionDomainName' }));
expect(stack).to(haveOutput({ outputName: 'OriginAccessIdentityS3Identifier' }));
Expand Down