From d5879b09e14f76e56c97c479ae13916e01902ade Mon Sep 17 00:00:00 2001 From: robertd Date: Mon, 22 Mar 2021 11:15:50 -0600 Subject: [PATCH] fix(cloudfront): auto-generated name of cache policy must be unique account-wide --- packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts | 4 ++-- packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts index e9dbf46901eda..a71569e89f02d 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts @@ -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'; @@ -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}'`); } diff --git a/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts b/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts index 4f6d618c51621..07d2752de4e2c 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts @@ -24,7 +24,7 @@ describe('CachePolicy', () => { expect(stack).toHaveResource('AWS::CloudFront::CachePolicy', { CachePolicyConfig: { - Name: 'StackCachePolicy0D6FCBC0', + Name: 'StackCachePolicy0D6FCBC0-testregion', MinTTL: 0, DefaultTTL: 86400, MaxTTL: 31536000,