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

fix(core): Add stage prefix to stack name shortening process #24443

Merged
merged 23 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8abb431
Add prefix to stack name shortening process
QustodioPablo Mar 3, 2023
668539c
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Mar 3, 2023
6c1532b
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Mar 6, 2023
7f06cd7
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Mar 21, 2023
b28f46f
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Mar 22, 2023
0cf310a
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Mar 28, 2023
b126e20
Merge branch 'QustodioPablo/fix-stack-name-too-long' of github.com:Qu…
QustodioPablo Mar 28, 2023
1f5b03b
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Mar 29, 2023
b151d21
Process prefix inside the makeUniqueResourceName function
QustodioPablo Mar 29, 2023
b1aee08
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Mar 30, 2023
ecfc9cb
Fixed scenario of Stage with only one default component
QustodioPablo Mar 30, 2023
297273a
Fixed missing brace
QustodioPablo Mar 30, 2023
16e96c1
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Mar 30, 2023
0e28b23
Use variable
QustodioPablo Mar 31, 2023
f52804e
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Mar 31, 2023
adecdbc
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Apr 3, 2023
f979e85
Recovered changes
QustodioPablo Apr 3, 2023
e9cd174
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Apr 4, 2023
c6c6053
Added prefix as option
QustodioPablo Apr 5, 2023
7655cd2
Merge branch 'QustodioPablo/fix-stack-name-too-long' of github.com:Qu…
QustodioPablo Apr 5, 2023
56bd828
Merge branch 'main' of https://github.com/aws/aws-cdk into QustodioPa…
QustodioPablo Apr 5, 2023
5dd29bc
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
QustodioPablo Apr 11, 2023
729f23e
Merge branch 'main' into QustodioPablo/fix-stack-name-too-long
mergify[bot] Apr 11, 2023
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
13 changes: 12 additions & 1 deletion packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ interface MakeUniqueResourceNameOptions {
* @default - none
*/
readonly allowedSpecialCharacters?: string;

/**
* Prefix to be added into the stack name
*
* @default - none
*/
readonly prefix?: string;
}

/**
Expand All @@ -49,6 +56,7 @@ const HASH_LEN = 8;
export function makeUniqueResourceName(components: string[], options: MakeUniqueResourceNameOptions) {
const maxLength = options.maxLength ?? 256;
const separator = options.separator ?? '';
const prefix = options.prefix ?? '';
components = components.filter(x => x !== HIDDEN_ID);

if (components.length === 0) {
Expand All @@ -59,7 +67,7 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique
// in order to support transparent migration of cloudformation templates to the CDK without the
// need to rename all resources.
if (components.length === 1) {
const topLevelResource = removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters);
const topLevelResource = prefix + removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters);

if (topLevelResource.length <= maxLength) {
return topLevelResource;
Expand All @@ -68,6 +76,9 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique

// Calculate the hash from the full path, included unresolved tokens so the hash value is always unique
const hash = pathHash(components);
if (prefix) {
components.unshift(prefix);
}
const human = removeDupes(components)
.filter(pathElement => pathElement !== HIDDEN_FROM_HUMAN_ID)
.map(pathElement => removeNonAllowedSpecialCharacters(pathElement, separator, options.allowedSpecialCharacters))
Expand Down
17 changes: 11 additions & 6 deletions packages/aws-cdk-lib/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ export class Stack extends Construct implements ITaggable {
private generateStackName() {
const assembly = Stage.of(this);
const prefix = (assembly && assembly.stageName) ? `${assembly.stageName}-` : '';
return `${prefix}${this.generateStackId(assembly)}`;
return `${this.generateStackId(assembly, prefix)}`;
}

/**
Expand All @@ -1439,7 +1439,7 @@ export class Stack extends Construct implements ITaggable {
/**
* Generate an ID with respect to the given container construct.
*/
private generateStackId(container: IConstruct | undefined) {
private generateStackId(container: IConstruct | undefined, prefix: string='') {
const rootPath = rootPathTo(this, container);
const ids = rootPath.map(c => Node.of(c).id);

Expand All @@ -1449,7 +1449,7 @@ export class Stack extends Construct implements ITaggable {
throw new Error('unexpected: stack id must always be defined');
}

return makeStackName(ids);
return makeStackName(ids, prefix);
}

private resolveExportedValue(exportedValue: any): ResolvedExport {
Expand Down Expand Up @@ -1635,9 +1635,14 @@ export function rootPathTo(construct: IConstruct, ancestor?: IConstruct): IConst
* has only one component. Otherwise we fall back to the regular "makeUniqueId"
* behavior.
*/
function makeStackName(components: string[]) {
if (components.length === 1) { return components[0]; }
return makeUniqueResourceName(components, { maxLength: 128 });
function makeStackName(components: string[], prefix: string='') {
if (components.length === 1) {
const stack_name = prefix + components[0];
if (stack_name.length <= 128) {
return stack_name;
}
}
return makeUniqueResourceName(components, { maxLength: 128, prefix: prefix });
}

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

test('generated stack names will not exceed 128 characters', () => {
// WHEN
const app = new App();
const stage = new Stage(app, 'ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTheStackName');
const stack = new BogusStack(stage, 'ThisStackNameIsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix');

// THEN
expect(stack.stackName.length).toEqual(128);
expect(stack.stackName).toEqual('ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix4CA9F65B');
});

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