"Parameters": { "ELBSubnets": { "Default": "subnet-5d8fea67,subnet-3e35cf15","Type": "CommaDelimitedList" },"LCKeyPair": { "Default": "key-master","Type": "String" },"LCSecurityGroups": { "Default": "sg-10a15c74,sg-880e5fec","Type": "CommaDelimitedList" } },
传递给子模板时,它们在同一模板上的此方法中被引用.
"ChildTempate1": { "Properties": { "Parameters": { "ELBSubnets": { "Ref": "ELBSubnets" },"KeyPair": { "Ref": "LCKeyPair" },"LCSecurityGroups": { "Ref": "LCSecurityGroups" } },
在子模板上,它们被声明完全相同.
"Parameters": { "ELBSubnets": { "Type": "CommaDelimitedList" },"LCKeyPair": { "Type": "String" },"LCSecurityGroups": { "Type": "CommaDelimitedList" } },
并且它们在子模板中的此方法中被引用.
"KeyName": { "Ref": "LCKeyPair" },"SecurityGroups": { "Fn::Join": [ ",",[ { "Ref": "LCSecurityGroups" } ] ] } },
这是模板的另一部分.
"Subnets": { "Fn::Join": [ ",[ { "Ref": "ELBSubnets" } ] ] } },
当我尝试在主模板上使用fn :: join时,它说
“Template validation error: Template error: every Fn::Join object requires two parameters,(1) a string delimiter and (2) a list of strings to be joined or a function that returns a list of strings (such as Fn::GetAZs) to be joined.”
当我不在主模板上使用fn :: join时,错误是
Value of property Parameters must be an object with String (or simple type) properties
无论我是否在子模板中使用fn :: join相同的参数.
这两个模板都可以在这里找到:https://github.com/slimg00dy/Troposphere-CloudformationTests
解决方法
因此,在主模板上,我使用Join(“,”)传递列表,使用Join(“”)传递字符串.这样它们被称为“a,c”和“String”
在子模板上,我将参数设置为列表的CommaDelimitedLists和String中的String.这是因为它们从主模板传递的方式.
然后我在子模板上使用Ref on the Child模板而不使用Join().这将CommaDelimitedLists创建为Lists,并将以Join(“”)连接的字符串作为字符串传递.
这是我在主模板上的参数声明.
"Parameters": { "ELBSubnets": { "Default": "subnet-5d8fea67,
这是我如何使用Join(“,”),Join(“”)和Ref.由于Ref将CommaDelimitedLists转换为Lists,因此我使用Join(“,”)将它们保存为CommaDelimitedLists并将它们传递给子模板.
至于KeyPair字符串,我确保它在父模板上被声明为CommaDelimitedList并与Join(“”)连接,这在引用子模板时有效地使其成为字符串.
"Parameters": { "ELBSubnets": { "Fn::Join": [ ",{ "Ref": "ELBSubnets" } ] },"LCKeyPair": { "Fn::Join": [ " ",{ "Ref": "LCKeyPair" } ] },"LCSecurityGroups": { "Fn::Join": [ ",{ "Ref": "LCSecurityGroups" } ] } },它们被声明为这样."Parameters": { "ELBSubnets": { "Type": "CommaDelimitedList" },并且它们都是正常引用而不使用子模板上的Join.
Subnets": { "Ref": "ELBSubnets" }可能有很多不同的方法可以做到这一点.我可以在子模板而不是父模板上进行连接.但是,我更喜欢保持一个模板复杂,其余模板尽可能干净.希望这有助于其他人.
我也应该能够将列表作为列表传递给子模板,然后我收到错误“Unknown parameter type:List”