Skip to content

Commit

Permalink
fix(cloudfront): auto-generated cache policy name might conflict cros…
Browse files Browse the repository at this point in the history
…s-region (aws#13737)

This commit changes the auto-generated name of the `CachePolicy` to include stack name and region, thus providing unique name for the `CachePolicy` account-wide.

Closes aws#13629.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
robertd authored and hollanddd committed Mar 31, 2021
1 parent 96871b9 commit e38f32c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duration, Names, Resource, Token } from '@aws-cdk/core';
import { Duration, Names, Resource, Stack, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnCachePolicy } from './cloudfront.generated';

Expand Down Expand Up @@ -125,7 +125,7 @@ export class CachePolicy extends Resource implements ICachePolicy {
physicalName: props.cachePolicyName,
});

const cachePolicyName = props.cachePolicyName ?? Names.uniqueId(this);
const cachePolicyName = props.cachePolicyName ?? `${Names.uniqueId(this)}-${Stack.of(this).region}`;
if (!Token.isUnresolved(cachePolicyName) && !cachePolicyName.match(/^[\w-]+$/i)) {
throw new Error(`'cachePolicyName' can only include '-', '_', and alphanumeric characters, got: '${props.cachePolicyName}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('CachePolicy', () => {

expect(stack).toHaveResource('AWS::CloudFront::CachePolicy', {
CachePolicyConfig: {
Name: 'StackCachePolicy0D6FCBC0',
Name: 'StackCachePolicy0D6FCBC0-testregion',
MinTTL: 0,
DefaultTTL: 86400,
MaxTTL: 31536000,
Expand Down

0 comments on commit e38f32c

Please sign in to comment.