diff --git a/packages/aws-cdk-lib/core/lib/stack.ts b/packages/aws-cdk-lib/core/lib/stack.ts index a31610fdfa00c..4d51504a48cc9 100644 --- a/packages/aws-cdk-lib/core/lib/stack.ts +++ b/packages/aws-cdk-lib/core/lib/stack.ts @@ -473,7 +473,7 @@ export class Stack extends Construct implements ITaggable { this._notificationArns = props.notificationArns ?? []; - if (!VALID_STACK_NAME_REGEX.test(this.stackName)) { + if (!Token.isUnresolved(this._stackName) && !VALID_STACK_NAME_REGEX.test(this.stackName)) { throw new Error(`Stack name must match the regular expression: ${VALID_STACK_NAME_REGEX.toString()}, got '${this.stackName}'`); } diff --git a/packages/aws-cdk-lib/core/test/stack.test.ts b/packages/aws-cdk-lib/core/test/stack.test.ts index 0f67d1ad6ac7b..77a83ba152919 100644 --- a/packages/aws-cdk-lib/core/test/stack.test.ts +++ b/packages/aws-cdk-lib/core/test/stack.test.ts @@ -38,6 +38,19 @@ describe('stack', () => { }).toThrow(`Stack name must be <= 128 characters. Stack name: '${reallyLongStackName}'`); }); + test('stack name can be a tokenized value', () => { + // GIVEN + const app = new App({}); + const stackName = Lazy.string({ produce: () => 'my-dynamic-string' }); + + // THEN + expect(() => { + new Stack(app, 'MyStack', { + stackName: stackName, + }); + }).not.toThrow(); + }); + test('stack objects have some template-level propeties, such as Description, Version, Transform', () => { const stack = new Stack(); stack.templateOptions.templateFormatVersion = 'MyTemplateVersion';