Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,7 @@ export class GlobalConfig implements t.TypeOf<typeof GlobalConfigTypes.globalCon
const aseaStacks: t.AseaStackInfo[] = [];
for (const account of mappingJson) {
this.externalLandingZoneResources?.accountsDeployedExternally.push(account.accountId);
let nestedStackPhysicalIds: string[] = [];
for (const stack of account.stacksAndResourceMapList) {
const phaseIdentifierIndex = stack.stackName.indexOf('Phase') + 5;
let phase = stack.stackName[phaseIdentifierIndex];
Expand All @@ -2249,6 +2250,11 @@ export class GlobalConfig implements t.TypeOf<typeof GlobalConfigTypes.globalCon
// Delete outputs from template. IMPORT_ASEA_RESOURCES stage will write required information into SSM Parameter Store.
// delete stack.template.Outputs;
await fs.promises.writeFile(templatePath, JSON.stringify(stack.template, null, 2));
for (const cfnResource of stack.resourceMap) {
if (cfnResource.resourceType == 'AWS::CloudFormation::Stack') {
nestedStackPhysicalIds.push(cfnResource.physicalResourceId);
}
}
aseaStacks.push({
accountId: account.accountId,
accountKey: account.accountKey,
Expand All @@ -2257,14 +2263,15 @@ export class GlobalConfig implements t.TypeOf<typeof GlobalConfigTypes.globalCon
resources: stack.resourceMap as t.CfnResourceType[],
templatePath,
phase,
/**
* ASEA creates Nested stacks only for Phase1 and Phase2 and "stack.stackName.substring(phaseIdentifierIndex)" doesn't include accountName in it.
* It is safe to use string include to check if stack is nestedStack or not.
* Other option is to check for resource type in "Resources.physicalResourceId" using stackName
*/
nestedStack: stack.stackName.substring(phaseIdentifierIndex).includes('NestedStack'),
nestedStack: stack.parentStack ? true : false,
});
}
for (const physicalId of nestedStackPhysicalIds) {
const nestedStack = aseaStacks.findIndex((stack) => physicalId.includes(`:stack/${stack.stackName}/`));
if (nestedStack > -1) {
aseaStacks[nestedStack].nestedStack = true;
}
}
}
return aseaStacks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,26 @@ export class ResourceMapping {
(stackAndResource): stackAndResource is Promise<StacksAndResourceMap> => stackAndResource !== undefined,
);
const stackAndResourcesList = await Promise.all(validStackAndResourcePromises);

const allNestedStacksPhysicalIds: string[] = stackAndResourcesList.flatMap((stack) =>
stack.resourceMap
.filter((resource) => resource.resourceType == 'AWS::CloudFormation::Stack')
.map((resource) => resource.physicalResourceId),
);

const allNestedStacks = stackAndResourcesList.filter((stackAndResource) =>
stackAndResource.stackName.includes('NestedStack'),
allNestedStacksPhysicalIds.find((physicalId) => physicalId.includes(`:stack/${stackAndResource.stackName}/`)),
);

const allNestedStackNames = allNestedStacks.map((stackAndResource) => stackAndResource.stackName);

stackAndResourcesList.forEach((stackAndResource) => {
const nestedStacks = this.getNestedStacks(stackAndResource, allNestedStacks);
if (nestedStacks) {
stackAndResource.nestedStacks = nestedStacks;
}
const parentStack = this.getParentStack(stackAndResource);
if (parentStack) {
stackAndResource.parentStack = parentStack;
}
if (!stackAndResource.stackName.includes('NestedStack')) {

if (!allNestedStackNames.includes(stackAndResource.stackName)) {
stackAndResourceMap.set(
`${stackAndResource.environment.accountId}-${stackAndResource.environment.region}-${stackAndResource.stackName}`,
stackAndResource,
Expand All @@ -291,14 +298,6 @@ export class ResourceMapping {
return stackAndResourceMap;
}

getParentStack(stackAndResource: StacksAndResourceMap) {
if (stackAndResource.stackName.includes('NestedStack')) {
const stackNameArr = stackAndResource.stackName.split('-');
return `${stackAndResource.environment.accountId}|${stackAndResource.environment.region}|${stackNameArr[0]}-${stackNameArr[1]}-${stackNameArr[2]}`;
}
return undefined;
}

getNestedStacks(stackAndResource: StacksAndResourceMap, nestedStacks: StacksAndResourceMap[]) {
const phaseIndex = stackAndResource.stackName.toLowerCase().indexOf('phase');
// checks the length of the stack to determine if it is the nested stack or parent stack
Expand Down
Loading