Skip to content

Commit 1b36124

Browse files
authored
feat(app-staging-synthesizer): option to specify staging stack name prefix (#26324)
Optionally specify a prefix for the staging stack name, which will be baked into the stackId and stackName. The default prefix is `StagingStack`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fd181c7 commit 1b36124

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ export interface DefaultStagingStackOptions {
111111
* @default true
112112
*/
113113
readonly autoDeleteStagingAssets?: boolean;
114+
115+
/**
116+
* Specify a custom prefix to be used as the staging stack name and
117+
* construct ID. The prefix will be appended before the appId, which
118+
* is required to be part of the stack name and construct ID to
119+
* ensure uniqueness.
120+
*
121+
* @default 'StagingStack'
122+
*/
123+
readonly stagingStackNamePrefix?: string;
114124
}
115125

116126
/**
@@ -159,12 +169,13 @@ export class DefaultStagingStack extends Stack implements IStagingResources {
159169
new UsingAppStagingSynthesizer(stack, `UsingAppStagingSynthesizer/${stack.stackName}`);
160170
}
161171

162-
const stackId = `StagingStack-${appId}-${context.environmentString}`;
172+
const stackPrefix = options.stagingStackNamePrefix ?? 'StagingStack';
173+
// Stack name does not need to contain environment because appId is unique inside an env
174+
const stackName = `${stackPrefix}-${appId}`;
175+
const stackId = `${stackName}-${context.environmentString}`;
163176
return new DefaultStagingStack(app, stackId, {
164177
...options,
165-
166-
// Does not need to contain environment because stack names are unique inside an env anyway
167-
stackName: `StagingStack-${appId}`,
178+
stackName,
168179
env: {
169180
account: stack.account,
170181
region: stack.region,

packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,29 @@ describe(AppStagingSynthesizer, () => {
492492
Template.fromJSON(getStagingResourceStack(asm).template).resourceCountIs('Custom::S3AutoDeleteObjects', 0);
493493
});
494494

495+
test('stack prefix can be customized', () => {
496+
// GIVEN
497+
const prefix = 'Prefix';
498+
app = new App({
499+
defaultStackSynthesizer: AppStagingSynthesizer.defaultResources({
500+
appId: APP_ID,
501+
stagingStackNamePrefix: prefix,
502+
}),
503+
});
504+
stack = new Stack(app, 'Stack', {
505+
env: {
506+
account: '000000000000',
507+
region: 'us-east-1',
508+
},
509+
});
510+
511+
// WHEN
512+
const asm = app.synth();
513+
514+
// THEN
515+
expect(getStagingResourceStack(asm, prefix).template).toBeDefined();
516+
});
517+
495518
describe('environment specifics', () => {
496519
test('throws if App includes env-agnostic and specific env stacks', () => {
497520
// GIVEN - App with Stack with specific environment
@@ -536,7 +559,7 @@ describe(AppStagingSynthesizer, () => {
536559
/**
537560
* Return the staging resource stack that is generated as part of the assembly
538561
*/
539-
function getStagingResourceStack(asm: CloudAssembly) {
540-
return asm.getStackArtifact(`StagingStack-${APP_ID}-000000000000-us-east-1`);
562+
function getStagingResourceStack(asm: CloudAssembly, prefix?: string) {
563+
return asm.getStackArtifact(`${prefix ?? 'StagingStack'}-${APP_ID}-000000000000-us-east-1`);
541564
}
542565
});

0 commit comments

Comments
 (0)