11import * as AWS from 'aws-sdk' ;
22import { ISDK } from './aws-auth' ;
3+ import { NestedStackNames } from './nested-stack-helpers' ;
34
45export interface ListStackResources {
56 listStackResources ( ) : Promise < AWS . CloudFormation . StackResourceSummary [ ] > ;
@@ -42,27 +43,33 @@ export interface ResourceDefinition {
4243}
4344
4445export interface EvaluateCloudFormationTemplateProps {
46+ readonly stackName : string ;
4547 readonly template : Template ;
4648 readonly parameters : { [ parameterName : string ] : string } ;
4749 readonly account : string ;
4850 readonly region : string ;
4951 readonly partition : string ;
5052 readonly urlSuffix : ( region : string ) => string ;
51- readonly listStackResources : ListStackResources ;
53+ readonly sdk : ISDK ;
54+ readonly nestedStackNames ?: { [ nestedStackLogicalId : string ] : NestedStackNames } ;
5255}
5356
5457export class EvaluateCloudFormationTemplate {
55- private readonly stackResources : ListStackResources ;
58+ private readonly stackName : string ;
5659 private readonly template : Template ;
5760 private readonly context : { [ k : string ] : any } ;
5861 private readonly account : string ;
5962 private readonly region : string ;
6063 private readonly partition : string ;
6164 private readonly urlSuffix : ( region : string ) => string ;
65+ private readonly sdk : ISDK ;
66+ private readonly nestedStackNames : { [ nestedStackLogicalId : string ] : NestedStackNames } ;
67+ private readonly stackResources : LazyListStackResources ;
68+
6269 private cachedUrlSuffix : string | undefined ;
6370
6471 constructor ( props : EvaluateCloudFormationTemplateProps ) {
65- this . stackResources = props . listStackResources ;
72+ this . stackName = props . stackName ;
6673 this . template = props . template ;
6774 this . context = {
6875 'AWS::AccountId' : props . account ,
@@ -74,22 +81,34 @@ export class EvaluateCloudFormationTemplate {
7481 this . region = props . region ;
7582 this . partition = props . partition ;
7683 this . urlSuffix = props . urlSuffix ;
84+ this . sdk = props . sdk ;
85+
86+ // We need names of nested stack so we can evaluate cross stack references
87+ this . nestedStackNames = props . nestedStackNames ?? { } ;
88+
89+ // The current resources of the Stack.
90+ // We need them to figure out the physical name of a resource in case it wasn't specified by the user.
91+ // We fetch it lazily, to save a service call, in case all hotswapped resources have their physical names set.
92+ this . stackResources = new LazyListStackResources ( this . sdk , this . stackName ) ;
7793 }
7894
7995 // clones current EvaluateCloudFormationTemplate object, but updates the stack name
80- public createNestedEvaluateCloudFormationTemplate (
81- listNestedStackResources : ListStackResources ,
96+ public async createNestedEvaluateCloudFormationTemplate (
97+ stackName : string ,
8298 nestedTemplate : Template ,
8399 nestedStackParameters : { [ parameterName : string ] : any } ,
84100 ) {
101+ const evaluatedParams = await this . evaluateCfnExpression ( nestedStackParameters ) ;
85102 return new EvaluateCloudFormationTemplate ( {
103+ stackName,
86104 template : nestedTemplate ,
87- parameters : nestedStackParameters ,
105+ parameters : evaluatedParams ,
88106 account : this . account ,
89107 region : this . region ,
90108 partition : this . partition ,
91109 urlSuffix : this . urlSuffix ,
92- listStackResources : listNestedStackResources ,
110+ sdk : this . sdk ,
111+ nestedStackNames : this . nestedStackNames ,
93112 } ) ;
94113 }
95114
@@ -262,20 +281,52 @@ export class EvaluateCloudFormationTemplate {
262281 return this . cachedUrlSuffix ;
263282 }
264283
284+ // Try finding the ref in the passed in parameters
265285 const parameterTarget = this . context [ logicalId ] ;
266286 if ( parameterTarget ) {
267287 return parameterTarget ;
268288 }
289+
290+ // If not in the passed in parameters, see if there is a default value in the template parameter that was not passed in
291+ const defaultParameterValue = this . template . Parameters ?. [ logicalId ] ?. Default ;
292+ if ( defaultParameterValue ) {
293+ return defaultParameterValue ;
294+ }
295+
269296 // if it's not a Parameter, we need to search in the current Stack resources
270297 return this . findGetAttTarget ( logicalId ) ;
271298 }
272299
273300 private async findGetAttTarget ( logicalId : string , attribute ?: string ) : Promise < string | undefined > {
301+
302+ // Handle case where the attribute is referencing a stack output (used in nested stacks to share parameters)
303+ // See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-cloudformation.html#w2ab1c17c23c19b5
304+ if ( logicalId === 'Outputs' && attribute ) {
305+ return this . evaluateCfnExpression ( this . template . Outputs [ attribute ] ?. Value ) ;
306+ }
307+
274308 const stackResources = await this . stackResources . listStackResources ( ) ;
275309 const foundResource = stackResources . find ( sr => sr . LogicalResourceId === logicalId ) ;
276310 if ( ! foundResource ) {
277311 return undefined ;
278312 }
313+
314+ if ( foundResource . ResourceType == 'AWS::CloudFormation::Stack' && attribute ?. startsWith ( 'Outputs.' ) ) {
315+ // need to resolve attributes from another stack's Output section
316+ const dependantStackName = this . nestedStackNames [ logicalId ] ?. nestedStackPhysicalName ;
317+ if ( ! dependantStackName ) {
318+ //this is a newly created nested stack and cannot be hotswapped
319+ return undefined ;
320+ }
321+ const dependantStackTemplate = this . template . Resources [ logicalId ] ;
322+ const evaluateCfnTemplate = await this . createNestedEvaluateCloudFormationTemplate (
323+ dependantStackName ,
324+ dependantStackTemplate ?. Properties ?. NestedTemplate ,
325+ dependantStackTemplate . newValue ?. Properties ?. Parameters ) ;
326+
327+ // Split Outputs.<refName> into 'Outputs' and '<refName>' and recursively call evaluate
328+ return evaluateCfnTemplate . evaluateCfnExpression ( { 'Fn::GetAtt' : attribute . split ( / \. ( .* ) / s) } ) ;
329+ }
279330 // now, we need to format the appropriate identifier depending on the resource type,
280331 // and the requested attribute name
281332 return this . formatResourceAttribute ( foundResource , attribute ) ;
0 commit comments