Skip to content

Commit

Permalink
feat(core): allow overriding the stage name (aws#22223)
Browse files Browse the repository at this point in the history
Currently the stage name is derived from the id of the stage and later becomes part of each stack name inside the stage:

https://github.com/aws/aws-cdk/blob/c425e8ca1a3d296eb6a7fd7e005d07c1eadd16aa/packages/%40aws-cdk/core/lib/stage.ts#L139

https://github.com/aws/aws-cdk/blob/c425e8ca1a3d296eb6a7fd7e005d07c1eadd16aa/packages/%40aws-cdk/core/lib/stack.ts#L1139-L1143

Since the ids in a scope must be different, deploying the same stage in the same scope to multiple regions would require different ids and would then lead to different stack names in the end. To keep stack names uniform across regions, a stack already allows customising the stack name uncoupled from the id:

https://github.com/aws/aws-cdk/blob/c425e8ca1a3d296eb6a7fd7e005d07c1eadd16aa/packages/%40aws-cdk/core/lib/stack.ts#L106-L111

This PR follows up on that concept and also allows customising the stage name.

This would then allow to deploy the same stage in the same scope to multiple regions while keeping the resulting stack names the same across regions.

----

### All Submissions:

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

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
danilobuerger authored and madeline-k committed Oct 10, 2022
1 parent 90a00c7 commit 0d0b239
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/core/lib/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export interface StageProps {
* temporary directory will be created.
*/
readonly outdir?: string;

/**
* Name of this stage.
*
* @default - Derived from the id.
*/
readonly stageName?: string;
}

/**
Expand Down Expand Up @@ -136,7 +143,7 @@ export class Stage extends Construct {
this.account = props.env?.account ?? this.parentStage?.account;

this._assemblyBuilder = this.createBuilder(props.outdir);
this.stageName = [this.parentStage?.stageName, id].filter(x => x).join('-');
this.stageName = [this.parentStage?.stageName, props.stageName ?? id].filter(x => x).join('-');
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/core/test/stage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ describe('stage', () => {
expect(stack.stackName).toEqual('MyStage-MyStack');
});

test('can override the stage name', () => {
// WHEN
const app = new App();
const stage = new Stage(app, 'NotMyStageName', { stageName: 'MyStage' });
const stack = new BogusStack(stage, 'MyStack');

// THEN
expect(stage.stageName).toEqual('MyStage');
expect(stack.stackName).toEqual('MyStage-MyStack');
});

test('Can not have dependencies to stacks outside the nested asm', () => {
// GIVEN
const app = new App();
Expand Down

0 comments on commit 0d0b239

Please sign in to comment.