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

fix(cloudfront): avoid to sort TTLs when using Tokens in CachePolicy #25920

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from 'constructs';
import { CfnCachePolicy } from './cloudfront.generated';
import { Duration, Names, Resource, Stack, Token } from '../../core';
import { Duration, Names, Resource, Stack, Token, withResolved } from '../../core';

/**
* Represents a Cache Policy
Expand Down Expand Up @@ -140,8 +140,15 @@ export class CachePolicy extends Resource implements ICachePolicy {
}

const minTtl = (props.minTtl ?? Duration.seconds(0)).toSeconds();
const defaultTtl = Math.max((props.defaultTtl ?? Duration.days(1)).toSeconds(), minTtl);
const maxTtl = Math.max((props.maxTtl ?? Duration.days(365)).toSeconds(), defaultTtl);
let defaultTtl = (props.defaultTtl ?? Duration.days(1)).toSeconds();
let maxTtl = (props.maxTtl ?? Duration.days(365)).toSeconds();

withResolved(defaultTtl, minTtl, () => {
defaultTtl = Math.max(defaultTtl, minTtl);
});
withResolved(maxTtl, defaultTtl, () => {
maxTtl = Math.max(maxTtl, defaultTtl);
});

const resource = new CfnCachePolicy(this, 'Resource', {
cachePolicyConfig: {
Expand Down
19 changes: 18 additions & 1 deletion packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Template } from '../../assertions';
import { App, Aws, Duration, Stack } from '../../core';
import { App, Aws, Duration, Lazy, Stack } from '../../core';
import { CachePolicy, CacheCookieBehavior, CacheHeaderBehavior, CacheQueryStringBehavior } from '../lib';

describe('CachePolicy', () => {
Expand Down Expand Up @@ -149,6 +149,23 @@ describe('CachePolicy', () => {
},
});
});

test('accepts tokens', () => {
new CachePolicy(stack, 'CachePolicy', {
cachePolicyName: 'MyPolicy',
minTtl: Duration.seconds(Lazy.number({ produce: () => 30 })),
defaultTtl: Duration.seconds(Lazy.number({ produce: () => 20 })),
maxTtl: Duration.seconds(Lazy.number({ produce: () => 10 })),
});

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::CachePolicy', {
CachePolicyConfig: {
MinTTL: 30,
DefaultTTL: 20,
MaxTTL: 10,
},
});
});
});
});

Expand Down