Skip to content

Commit

Permalink
feat(core): throw error when stack name exceeds max length (#19725)
Browse files Browse the repository at this point in the history
https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html

----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
peterwoodworth committed Apr 2, 2022
1 parent 7e9a43d commit 1ffd45e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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),
});

pipeline.addStage(myApp, {
Expand Down

0 comments on commit 1ffd45e

Please sign in to comment.