Skip to content

Commit

Permalink
working nested stack parsing, I think...
Browse files Browse the repository at this point in the history
  • Loading branch information
comcalvi committed Jan 8, 2024
1 parent 7543775 commit 441993e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/aws-cdk/lib/api/nested-stack-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { CloudFormation } from 'aws-sdk';
import * as fs from 'fs-extra';
import { ISDK } from './aws-auth';
import { LazyListStackResources, ListStackResources } from './evaluate-cloudformation-template';
Expand All @@ -20,6 +21,14 @@ export interface TemplateWithNestedStackCount {
readonly nestedStackCount: number;
}

export type NestedChangeSet = CloudFormation.DescribeChangeSetOutput & {
Changes?: CloudFormation.Changes & {
ResourceChange?: {
NestedChanges?: CloudFormation.DescribeChangeSetOutput | NestedChangeSet,
},
}[],
};

/**
* Reads the currently deployed template from CloudFormation and adds a
* property, `NestedTemplate`, to any nested stacks that appear in either
Expand Down Expand Up @@ -68,6 +77,27 @@ export async function loadCurrentTemplate(
return loadCurrentStackTemplate(stackArtifact.stackName, sdk, retrieveProcessedTemplate);
}

/**
* inlines changesets of all nested stacks in rootChangeSet into rootChangeSet
*/
export async function populateNestedChangeSet(rootChangeSet: NestedChangeSet, cfn: CloudFormation) {
const changes = rootChangeSet.Changes;
if (changes) {
for (const change of changes) {
if (change.Type === 'Resource' && change.ResourceChange) {
if (change.ResourceChange && change.ResourceChange.ResourceType === 'AWS::CloudFormation::Stack') {
const nestedChangeSetId = change.ResourceChange.ChangeSetId;
const nestedChangeSet = await cfn.describeChangeSet({
ChangeSetName: nestedChangeSetId!,
}).promise();
await populateNestedChangeSet(nestedChangeSet, cfn);
change.ResourceChange.NestedChanges = nestedChangeSet;
}
}
}
}
}

async function loadCurrentStackTemplate(
stackName: string, sdk: ISDK, retrieveProcessedTemplate: boolean = false,
) : Promise<Template> {
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk/lib/api/util/cloudformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { debug } from '../../logging';
import { deserializeStructure } from '../../serialize';
import { SdkProvider } from '../aws-auth';
import { Deployments } from '../deployments';
import { populateNestedChangeSet } from '../nested-stack-helpers';

export type Template = {
Parameters?: Record<string, TemplateParameter>;
Expand Down Expand Up @@ -382,6 +383,7 @@ async function createChangeSet(options: CreateChangeSetOptions): Promise<CloudFo
debug('Initiated creation of changeset: %s; waiting for it to finish creating...', changeSet.Id);
// Fetching all pages if we'll execute, so we can have the correct change count when monitoring.
const createdChangeSet = await waitForChangeSet(options.cfn, options.stack.stackName, options.changeSetName, { fetchAll: options.willExecute });
await populateNestedChangeSet(createdChangeSet, options.cfn);
await cleanupOldChangeset(options.exists, options.changeSetName, options.stack.stackName, options.cfn);

return createdChangeSet;
Expand Down

0 comments on commit 441993e

Please sign in to comment.