@@ -289,10 +289,62 @@ export class CfnInclude extends core.CfnElement {
289
289
return ret ;
290
290
}
291
291
292
+ /**
293
+ * Includes a template for a child stack inside of this parent template.
294
+ * A child with this logical ID must exist in the template,
295
+ * and be of type AWS::CloudFormation::Stack.
296
+ * This is equivalent to specifying the value in the {@link CfnIncludeProps.nestedStacks}
297
+ * property on object construction.
298
+ *
299
+ * @param logicalId the ID of the stack to retrieve, as it appears in the template
300
+ * @param nestedStackProps the properties of the included child Stack
301
+ * @returns the same {@link IncludedNestedStack} object that {@link getNestedStack} returns for this logical ID
302
+ */
303
+ public includeNestedStack ( logicalId : string , nestedStackProps : CfnIncludeProps ) : IncludedNestedStack {
304
+ if ( logicalId in this . nestedStacks ) {
305
+ throw new Error ( `Nested Stack '${ logicalId } ' was already included in its parent template` ) ;
306
+ }
307
+ const cfnStack = this . resources [ logicalId ] ;
308
+ if ( ! cfnStack ) {
309
+ throw new Error ( `Nested Stack with logical ID '${ logicalId } ' was not found in the template` ) ;
310
+ }
311
+ if ( cfnStack instanceof core . CfnStack ) {
312
+ // delete the old CfnStack child - one will be created by the NestedStack object
313
+ this . node . tryRemoveChild ( logicalId ) ;
314
+
315
+ const self = this ;
316
+ const finder : cfn_parse . ICfnFinder = {
317
+ findResource ( lId ) : core . CfnResource | undefined {
318
+ return self . resources [ lId ] ;
319
+ } ,
320
+ findRefTarget ( elementName : string ) : core . CfnElement | undefined {
321
+ return self . resources [ elementName ] ?? self . parameters [ elementName ] ;
322
+ } ,
323
+ findCondition ( conditionName : string ) : core . CfnCondition | undefined {
324
+ return self . conditions [ conditionName ] ;
325
+ } ,
326
+ findMapping ( mappingName ) : core . CfnMapping | undefined {
327
+ return self . mappings [ mappingName ] ;
328
+ } ,
329
+ } ;
330
+ const cfnParser = new cfn_parse . CfnParser ( {
331
+ finder,
332
+ parameters : this . parametersToReplace ,
333
+ } ) ;
334
+ // createNestedStack() expects this to be filled
335
+ this . nestedStacksToInclude [ logicalId ] = nestedStackProps ;
336
+ this . createNestedStack ( logicalId , cfnParser ) ;
337
+ return this . nestedStacks [ logicalId ] ;
338
+ } else {
339
+ throw new Error ( `Resource with logical ID '${ logicalId } ' is not a CloudFormation Stack` ) ;
340
+ }
341
+ }
342
+
292
343
/**
293
344
* Returns the NestedStack with name logicalId.
294
- * For a nested stack to be returned by this method, it must be specified in the {@link CfnIncludeProps.nestedStacks}
295
- * property.
345
+ * For a nested stack to be returned by this method,
346
+ * it must be specified either in the {@link CfnIncludeProps.nestedStacks} property,
347
+ * or through the {@link includeNestedStack} method.
296
348
*
297
349
* @param logicalId the ID of the stack to retrieve, as it appears in the template
298
350
*/
0 commit comments