cloudformation-template-generator icon indicating copy to clipboard operation
cloudformation-template-generator copied to clipboard

Typing issues when nesting templates

Open bkrodgers opened this issue 9 years ago • 1 comments

CFTG's type safety starts causing problems when trying to reference one template as a stack inside another. An example of this is if you want to have your core VPC structure in one template, include that as a AWS::CloudFormation::Stack, and then put instances into the VPC's subnets from your main template.

To do this in regular CFN, you would expose the subnet IDs as Outputs in the VPC template, and then reference them with a GetAtt, such as like this:

"SubnetId": {
     "Fn::GetAtt": ["VPCStack", "Outputs.PrivateSubnet2"]
}

However, our implementation of AWS::EC2::Instance expects Subnet ID as follows:

SubnetId:               Token[ResourceRef[`AWS::EC2::Subnet`]]

Our implementation of Fn::GetAtt is typed only to String, so we can't pass it in as something that meets the AWS::EC2::Subnet criteria.

This is an issue throughout the library -- anywhere where you may want to reference something from a parent template, if that resource's parameter is typed, you're out of luck. There's an assumption in the library that you're going to reference something you've defined within the template, and thus you'll have a typed reference to pass in. That's no longer the case when we start nesting and want to reference things with GetAtt

Of course none of this is a problem in CloudFormation itself, since all of this serializes down to simple strings in the JSON output. This is just an issue with our stronger typing.

bkrodgers avatar Jul 14 '16 22:07 bkrodgers

One way to solve this would be to do what Fn::FindInMap does and accept a type parameter. Rather than change the existing GetAtt, we could add a new Fn::GetAttFromStack that does this. It could also automatically add in the Output. prefix to the output from the nested stack as well, as described here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html

bkrodgers avatar Jul 14 '16 22:07 bkrodgers