@@ -14,6 +14,20 @@ export interface CfnIncludeProps {
14
14
*/
15
15
readonly templateFile : string ;
16
16
17
+ /**
18
+ * Whether the resources should have the same logical IDs in the resulting CDK template
19
+ * as they did in the original CloudFormation template file.
20
+ * If you're vending a Construct using an existing CloudFormation template,
21
+ * make sure to pass this as `false`.
22
+ *
23
+ * **Note**: regardless of whether this option is true or false,
24
+ * the {@link CfnInclude.getResource} and related methods always uses the original logical ID of the resource/element,
25
+ * as specified in the template file.
26
+ *
27
+ * @default true
28
+ */
29
+ readonly preserveLogicalIds ?: boolean ;
30
+
17
31
/**
18
32
* Specifies the template files that define nested stacks that should be included.
19
33
*
@@ -86,8 +100,7 @@ export class CfnInclude extends core.CfnElement {
86
100
// read the template into a JS object
87
101
this . template = futils . readYamlSync ( props . templateFile ) ;
88
102
89
- // ToDo implement preserveLogicalIds=false
90
- this . preserveLogicalIds = true ;
103
+ this . preserveLogicalIds = props . preserveLogicalIds ?? true ;
91
104
92
105
// check if all user specified parameter values exist in the template
93
106
for ( const logicalId of Object . keys ( this . parametersToReplace ) ) {
@@ -326,7 +339,7 @@ export class CfnInclude extends core.CfnElement {
326
339
mapping : cfnParser . parseValue ( this . template . Mappings [ mappingName ] ) ,
327
340
} ) ;
328
341
this . mappings [ mappingName ] = cfnMapping ;
329
- cfnMapping . overrideLogicalId ( mappingName ) ;
342
+ this . overrideLogicalIdIfNeeded ( cfnMapping , mappingName ) ;
330
343
}
331
344
332
345
private createParameter ( logicalId : string ) : void {
@@ -357,7 +370,7 @@ export class CfnInclude extends core.CfnElement {
357
370
noEcho : expression . NoEcho ,
358
371
} ) ;
359
372
360
- cfnParameter . overrideLogicalId ( logicalId ) ;
373
+ this . overrideLogicalIdIfNeeded ( cfnParameter , logicalId ) ;
361
374
this . parameters [ logicalId ] = cfnParameter ;
362
375
}
363
376
@@ -384,7 +397,7 @@ export class CfnInclude extends core.CfnElement {
384
397
assertions : ruleProperties . Assertions ,
385
398
} ) ;
386
399
this . rules [ ruleName ] = rule ;
387
- rule . overrideLogicalId ( ruleName ) ;
400
+ this . overrideLogicalIdIfNeeded ( rule , ruleName ) ;
388
401
}
389
402
390
403
private createOutput ( logicalId : string , scope : core . Construct ) : void {
@@ -422,7 +435,7 @@ export class CfnInclude extends core.CfnElement {
422
435
} ) ( ) ,
423
436
} ) ;
424
437
425
- cfnOutput . overrideLogicalId ( logicalId ) ;
438
+ this . overrideLogicalIdIfNeeded ( cfnOutput , logicalId ) ;
426
439
this . outputs [ logicalId ] = cfnOutput ;
427
440
}
428
441
@@ -455,8 +468,7 @@ export class CfnInclude extends core.CfnElement {
455
468
expression : cfnParser . parseValue ( this . template . Conditions [ conditionName ] ) ,
456
469
} ) ;
457
470
458
- // ToDo handle renaming of the logical IDs of the conditions
459
- cfnCondition . overrideLogicalId ( conditionName ) ;
471
+ this . overrideLogicalIdIfNeeded ( cfnCondition , conditionName ) ;
460
472
this . conditions [ conditionName ] = cfnCondition ;
461
473
return cfnCondition ;
462
474
}
@@ -533,11 +545,7 @@ export class CfnInclude extends core.CfnElement {
533
545
}
534
546
}
535
547
536
- if ( this . preserveLogicalIds ) {
537
- // override the logical ID to match the original template
538
- l1Instance . overrideLogicalId ( logicalId ) ;
539
- }
540
-
548
+ this . overrideLogicalIdIfNeeded ( l1Instance , logicalId ) ;
541
549
this . resources [ logicalId ] = l1Instance ;
542
550
return l1Instance ;
543
551
}
@@ -585,4 +593,10 @@ export class CfnInclude extends core.CfnElement {
585
593
}
586
594
return ret ;
587
595
}
596
+
597
+ private overrideLogicalIdIfNeeded ( element : core . CfnElement , id : string ) : void {
598
+ if ( this . preserveLogicalIds ) {
599
+ element . overrideLogicalId ( id ) ;
600
+ }
601
+ }
588
602
}
0 commit comments