Skip to content

Commit f2c5f26

Browse files
authored
fix(cloudfront): fix validation for unresolved webAclId tokens (#34102)
### Issue # (if applicable) Closes #34099. ### Reason for this change Fixes a bug where CloudFront `Distribution` validation would throw a `TypeError` when `webAclId` was a CloudFormation token or intrinsic, instead of a plain string. This prevented users from passing unresolved tokens or references as `webAclId`. ### Description of changes - Updated `validateWebAclId()` to **skip validation** if `webAclId` is **not a string** or is an **unresolved token**. - This prevents `.startsWith()` from being called on non-string values, avoiding runtime errors. - Added unit tests to cover unresolved tokens and non-string values. - Removed the integration test that attempted to deploy dummy WebACL ARNs, which caused CloudFront deployment failures due to invalid account ownership. This change enables users to safely pass CloudFormation references or tokens as `webAclId` without causing synthesis errors. ### Describe any new or updated permissions being added None. ### Description of how you validated changes - Added and updated unit tests for `validateWebAclId()` to cover tokens and non-string values. - Removed the failing integration test that was not suitable for automated deployment. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent baa4a5c commit f2c5f26

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts

+4
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ export class Distribution extends Resource implements IDistribution {
663663
}
664664

665665
private validateWebAclId(webAclId: string) {
666+
if (Token.isUnresolved(webAclId)) {
667+
// Cannot validate unresolved tokens or non-string values at synth-time.
668+
return;
669+
}
666670
if (webAclId.startsWith('arn:')) {
667671
const webAclRegion = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME).region;
668672
if (!Token.isUnresolved(webAclRegion) && webAclRegion !== 'us-east-1') {

packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as iam from '../../aws-iam';
66
import * as kinesis from '../../aws-kinesis';
77
import * as lambda from '../../aws-lambda';
88
import * as s3 from '../../aws-s3';
9-
import { App, Aws, Duration, Stack } from '../../core';
9+
import { App, Aws, Duration, Stack, Token } from '../../core';
1010
import {
1111
AllowedMethods,
1212
CfnDistribution,
@@ -1432,6 +1432,18 @@ describe('attachWebAclId', () => {
14321432
});
14331433
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
14341434
});
1435+
1436+
test('does not validate unresolved token webAclId', () => {
1437+
const origin = defaultOrigin();
1438+
1439+
const distribution = new Distribution(stack, 'MyDist', {
1440+
defaultBehavior: { origin },
1441+
webAclId: Token.asString({ Ref: 'SomeWebAcl' }), // unresolved token
1442+
});
1443+
1444+
// Should synthesize without error
1445+
Template.fromStack(stack);
1446+
});
14351447
});
14361448
});
14371449

0 commit comments

Comments
 (0)