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

feat(core): throw error when stack name exceeds max length #19725

Merged
merged 3 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 3 additions & 0 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ export class Stack extends CoreConstruct implements ITaggable {
}

this._stackName = props.stackName ?? this.generateStackName();
if (this._stackName.length > 128) {
throw new Error(`Stack name must be <= 128 characters. Stack name: '${this._stackName}'`);
}
this.tags = new TagManager(TagType.KEY_VALUE, 'aws:cdk:stack', props.tags);

if (!VALID_STACK_NAME_REGEX.test(this.stackName)) {
Expand Down
13 changes: 13 additions & 0 deletions packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('stack', () => {
expect(toCloudFormation(stack)).toEqual({ });
});

test('stack name cannot exceed 128 characters', () => {
// GIVEN
const app = new App({});
const reallyLongStackName = 'LookAtMyReallyLongStackNameThisStackNameIsLongerThan128CharactersThatIsNutsIDontThinkThereIsEnoughAWSAvailableToLetEveryoneHaveStackNamesThisLong';

// THEN
expect(() => {
new Stack(app, 'MyStack', {
stackName: reallyLongStackName,
});
}).toThrow(`Stack name must be <= 128 characters. Stack name: '${reallyLongStackName}'`);
});

test('stack objects have some template-level propeties, such as Description, Version, Transform', () => {
const stack = new Stack();
stack.templateOptions.templateFormatVersion = 'MyTemplateVersion';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ test('timeout from defaults can be overridden', () => {
test('envFromOutputs works even with very long stage and stack names', () => {
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');

const myApp = new AppWithOutput(app, 'Alpha'.repeat(20), {
stackId: 'Stack'.repeat(20),
const myApp = new AppWithOutput(app, 'Alpha'.repeat(10), {
stackId: 'Stack'.repeat(10),
Comment on lines -128 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was failing because it will create a stack with a 200 character name :p

});

pipeline.addStage(myApp, {
Expand Down