@@ -70,14 +70,48 @@ export class CdkFramework implements IFramework {
7070 JSON . stringify ( lambdasInCdk , null , 2 ) ,
7171 ) ;
7272
73- //get all stack names
74- const stackNames = [
75- ...new Set ( // unique
73+ const cdkTokenRegex = / ^ \$ { Token\[ T O K E N \. \d + \] } $ / ;
74+ const stackTokensCdkPathMappings = Object . fromEntries (
75+ lambdasInCdk
76+ . filter ( ( lambda ) => cdkTokenRegex . test ( lambda . stackName ) )
77+ . map ( ( lambda ) => [ lambda . stackName , lambda . stackCdkPath ] ) ,
78+ ) ;
79+ const realStackNames = [
80+ ...new Set (
7681 lambdasInCdk . map ( ( lambda ) => {
77- return lambda . stackName ;
82+ return lambda . rootStackName ;
7883 } ) ,
7984 ) ,
8085 ] ;
86+
87+ const unresolvedTokens = new Set ( Object . keys ( stackTokensCdkPathMappings ) ) ;
88+ const resolvedTokenizedStackNames = new Set < string > ( ) ;
89+
90+ if ( Object . keys ( stackTokensCdkPathMappings ) . length ) {
91+ Logger . verbose (
92+ `[CDK] Found tokenized stackNames: ${ [ ...unresolvedTokens ] . join ( ', ' ) } ` ,
93+ ) ;
94+ Logger . verbose (
95+ `[CDK] Will look for tokenized stackNames in ${ realStackNames . join (
96+ ', ' ,
97+ ) } `,
98+ ) ;
99+ for ( const realStackName of realStackNames ) {
100+ if ( ! unresolvedTokens . size ) break ;
101+ await this . resolveTokenizedStackNames (
102+ unresolvedTokens ,
103+ resolvedTokenizedStackNames ,
104+ stackTokensCdkPathMappings ,
105+ awsConfiguration ,
106+ realStackName ,
107+ ) ;
108+ }
109+ }
110+
111+ //get all stack names
112+ const stackNames = [
113+ ...new Set ( realStackNames . concat ( [ ...resolvedTokenizedStackNames ] ) ) , // unique
114+ ] ;
81115 Logger . verbose (
82116 `[CDK] Found the following stacks in CDK: ${ stackNames . join ( ', ' ) } ` ,
83117 ) ;
@@ -170,6 +204,61 @@ export class CdkFramework implements IFramework {
170204 return lambdasDiscovered ;
171205 }
172206
207+ protected async resolveTokenizedStackNames (
208+ unresolvedTokens : Set < string > ,
209+ resolvedTokenizedStackNames : Set < string > ,
210+ stackTokensCdkPathMappings : Record < string , string > ,
211+ awsConfiguration : AwsConfiguration ,
212+ stackName : string ,
213+ ) : Promise < void > {
214+ if ( ! unresolvedTokens . size ) {
215+ return ;
216+ }
217+
218+ const cfTemplate = await CloudFormation . getCloudFormationStackTemplate (
219+ stackName ,
220+ awsConfiguration ,
221+ ) ;
222+
223+ if ( cfTemplate ) {
224+ const nestedStacks = Object . entries ( cfTemplate . Resources )
225+ . filter (
226+ ( [ , resource ] : [ string , any ] ) =>
227+ resource . Type === 'AWS::CloudFormation::Stack' ,
228+ )
229+ . map ( ( [ key , resource ] : [ string , any ] ) => {
230+ return {
231+ logicalId : key ,
232+ cdkPath : resource . Metadata [ 'aws:cdk:path' ] ,
233+ } ;
234+ } ) ;
235+
236+ for ( const nestedStack of nestedStacks ) {
237+ const mapping = Object . entries ( stackTokensCdkPathMappings ) . find (
238+ ( f ) => f [ 1 ] === nestedStack . cdkPath ,
239+ ) ;
240+
241+ if ( mapping ) {
242+ unresolvedTokens . delete ( mapping [ 0 ] ) ;
243+ const physicalResourceId =
244+ await CloudFormation . getStackResourcePhysicalResourceId (
245+ stackName ,
246+ nestedStack . logicalId ,
247+ awsConfiguration ,
248+ ) ;
249+ resolvedTokenizedStackNames . add ( physicalResourceId ) ;
250+ await this . resolveTokenizedStackNames (
251+ unresolvedTokens ,
252+ resolvedTokenizedStackNames ,
253+ stackTokensCdkPathMappings ,
254+ awsConfiguration ,
255+ physicalResourceId ,
256+ ) ;
257+ }
258+ }
259+ }
260+ }
261+
173262 /**
174263 * Getz Lambda functions from the CloudFormation template metadata
175264 * @param stackName
@@ -308,9 +397,15 @@ export class CdkFramework implements IFramework {
308397 `;
309398 global.lambdas = global.lambdas ?? [];
310399
400+ let rootStack = this.stack;
401+ while (rootStack.nestedStackParent) {
402+ rootStack = rootStack.nestedStackParent;
403+ }
311404 const lambdaInfo = {
312405 //cdkPath: this.node.defaultChild?.node.path ?? this.node.path,
406+ stackCdkPath: this.stack.node.defaultChild?.node.path ?? this.stack.node.path,
313407 stackName: this.stack.stackName,
408+ rootStackName: rootStack.stackName,
314409 codePath: props.entry,
315410 code: props.code,
316411 node: this.node,
@@ -320,6 +415,8 @@ export class CdkFramework implements IFramework {
320415
321416 // console.log("CDK INFRA: ", {
322417 // stackName: lambdaInfo.stackName,
418+ // stackCdkPath: lambdaInfo.stackCdkPath,
419+ // rootStackName: lambdaInfo.rootStackName,
323420 // codePath: lambdaInfo.codePath,
324421 // code: lambdaInfo.code,
325422 // handler: lambdaInfo.handler,
@@ -490,6 +587,8 @@ export class CdkFramework implements IFramework {
490587 return {
491588 cdkPath : lambda . cdkPath ,
492589 stackName : lambda . stackName ,
590+ stackCdkPath : lambda . stackCdkPath ,
591+ rootStackName : lambda . rootStackName ,
493592 packageJsonPath,
494593 codePath,
495594 handler,
@@ -572,6 +671,8 @@ export class CdkFramework implements IFramework {
572671 return lambdas as {
573672 cdkPath : string ;
574673 stackName : string ;
674+ stackCdkPath : string ;
675+ rootStackName : string ;
575676 codePath ?: string ;
576677 code : {
577678 path ?: string ;
0 commit comments