问题描述
我曾经有一个 CloudFormation 堆栈,它传递 VpcId 和一个 List<AWS::EC2::subnet>
(子网列表)。但后来我决定我实际上希望我的堆栈创建自己的 VPC。所以我想出了这个:
{
"PublicsubnetOne": {
"Type": "AWS::EC2::subnet","Properties": {
"VpcId": {"Ref": "VPC"},"CidrBlock": "10.0.0.0/24","AvailabilityZone": {
"Fn::Select": ["0",{"Fn::GetAZs": ""}]
}
}
},"PublicsubnetTwo": {
"Type": "AWS::EC2::subnet","AvailabilityZone": {
"Fn::Select": ["1","ApplicationLoadBalancer" : {
"Type" : "AWS::ElasticLoadBalancingV2::LoadBalancer","Properties" : {
"subnets" : { "Ref": "PublicsubnetOne,PublicsubnetTwo"}
}
},}
这会导致以下错误:
未解决的资源依赖关系 [PublicsubnetOne,PublicsubnetTwo] 在模板的 Resources 块中
如何正确地将 List<>
的 Refs 传递给属性?
解决方法
试试这个:
{"Ref": "PublicSubnetOne,PublicSubnetTwo"}[{"Ref": "PublicSubnetOne"},{"Ref": "PublicSubnetTwo"}]
当您说 {"Ref": "PublicSubnetOne,PublicSubnetTwo"}
时,CloudFormation 会逐字查找名称为 "PublicSubnetOne,PublicSubnetTwo"
的内容。
您可能对此感到困惑,因为 it's totally fine to pass a Parameter with --parameter-overrides (from aws cloudformation deploy
) with the value "vpc-someidhere,vpc-someidhere2"
,但它不适用于 refs。如果 Parameter
的 Type
是 List<>
There's also an answer that has similar keywords 实际上建议将 (Fn::Join) 两个 ID 连接在一起,但是我遇到了类似 Subnets should be List of String
的错误