CloudFormation String Help

Hello,

I am attempting to do some work in the middle of a string and having a hard go of it.

I have been playing with Fn::Sub and Fn::ImportValue without much luck.

Essentially I am carrying in a Parameter from a different CloudFormation scripts Output.

What I am attempting to land at is:

“Name”: “ResourceTypeStage”,

I have tried

“Name”: {“Fn::Sub”: “ResourceType${ParentCFNStack}-Environment”},

But that gives me the name of the parent stack, not the value of that variable.

I tried wrapping that in Fn::ImportValue, but that didn’t work either.

Following some docos I found online I tried:

“Name”: { “Fn::Sub”: [ “ResourceType${Env}”, { “Env”: {“Ref” : ${ParentCFNStack}-Environment }} ]} but that errored out as well.

Any help would be greatly appreciated.


Related discussions:

I’m a bit confused. How is the variable being passed to the child stack?

Sorry about that, I am new to Cloudformation so my explanation is probably a little bunk. :slight_smile:

The parent has it in its Outputs:

“Outputs”: {
“Environment”: {
“Description”: “Environment (stage,production) associated with this CFN template”,
“Value”: “Stage”,
“Export”: {
“Name”: {“Fn::Sub”: “${AWS::StackName}-Environment”}
}
},
}

My CF has it referenced as a Parameter:

“Parameters”:{
“ParentCFNStack”:{
“Default”: “as-vpc-stage-cfn-stack”,
“Description”: “Stack name of parent VPC, typically nn-vpc-igw-igwa-cfn…”,
“Type”: “String”
}
},

Try this:

“Fn::ImportValue”: {
“Fn::Sub”: “${ParentCFNStack}-Environment”
}

If you’re just trying to reference data from a parent stack, just pass it as a parameter into your nested stack. No need to export/import in a parent/child stack.

This^

yeah i follow that, but how do i nest that inside of the string?

“Name”: “stringValue{ “Fn::ImportValue”: { “Fn::Sub”: “${ParentCFNStack}-Environment” } }”

?

Do i need to use a Join instead?

Did some digging online about this.

Do you mean by using something like this in the child?

“Parameters”: { “vpcid”: { “Fn::GetAtt”: [“VPCStack”,“Outputs.VPCID”] } }

I still need to define as outputs in parent right?

I’m not sure what you’re asking about. You want to take the import value, and put it in another string, right?

Yeah in that case I think you need to use a join. Not sure if you can use a sub inside of a sub.

Thanks for the help. Thats what did it.

“Name”: {“Fn::Join”: [“”,[“ResoureType”,{“Fn::ImportValue”: {“Fn::Sub”: “${ParentCFNStack}-Environment”}}]]},

Resulted in the name being ResourceTypeStage like i was looking for.

Close. You don’t need to use outputs at all. You just need to specify the parameter value in the AWS::CloudFormation::Stack resource that you’re creating in the parent stack. Also, in case it’s not clear, you need to define a parameter in the child stack in order to pass it a value. Whatever syntax you are currently using to build that output value can simply be input into the child stack resource parameter, which will pass that to the child stack.

I’m currently on mobile, so can’t really give a good example, but hope that makes sense.

Ah, thanks for the pointer at AWS::CloudFormation::Stack

I guess we are using some sort of “faked” nesting lol as AWS::CloudFormation::Stack currently isn’t in use. The “Parent” has outputs that the child references and then the child’s child is referencing the child.

Looks like this needs a complete overhaul to take advantage of the Stack Nesting & Parameters

Really appreciate the help!

Ah, in that case import/export is the way to go. VPC, as I see in your example, is a good use case for that. Just keep in mind that once you’ve imported an exported value it can’t be updated.

Best of luck with your stacks