AWS Cloudformation:Fn ::在Fn :: FindInMap语句中加入?

时间:2014-05-23 10:22:07

标签: templates amazon-web-services amazon-cloudformation

尝试在Fn :: FindInMap中使用Fn :: Join,如下所示:

"SubnetId": {
    "Fn::FindInMap": [
        {
            "Ref": "OrganizationName"
        },
        "AZ",
        {
            "Fn::Join": [
                "",
                [
                    {
                        "Ref": "Environment"
                    },
                    {
                        "Ref": "Member1AZ"
                    }
                ]
            ]
        }
    ]
}

OrganizationName,Environment和Member1AZ都是参数。基本上它应该连接到我的映射并生成,例如:

"SubnetId" : { "Fn::FindInMap" : [ "Organization2", "AZ", "prod1c" ]}

但是,它似乎没有将Fn :: Join的输出作为Fn :: FindInMap上的单个实体,如果我硬编码模板的那一部分,它会正确验证。

A client error (ValidationError) occurred when calling the ValidateTemplate operation: Template error: every Fn::FindInMap object requires three parameters, the map name, map key and the attribute for return value

我的映射如下:

Mappings" : {
      "OrganizationDefaults" : {
            "AZ" : {
                "prod1a" : "subnet-foobar1",
                "qa1a" : "subnet-foobar2",
                "prod1c" : "subnet-foobar3",
                "qa1c" : "subnet-foobar4"
            }
      },
      "OrganizationTwo" : {
            "AZ" : {
                "prod1a" : "subnet-foobar5",
                "qa1a" : "subnet-foobar6",
                "prod1c" : "subnet-foobar7",
                "qa1c" : "subnet-foobar8"
            }
      },
},

任何人都可以提供帮助吗,或者之前不得不做类似的事情?我需要为列出的任何组织使用相同的模板,所以如果我能做对,Mappings应该为我解决这个问题。

2 个答案:

答案 0 :(得分:4)

虽然我同意@Jason,在您的情况下,您的地图布局的重构是最适合您的解决方案,但有些情况下CloudFormation中地图的2D限制可能会受到限制,因此我将在此处发布可能的解决方案

截至本文发布之日,Fn::FindInMap内在函数仅支持以下嵌套函数:

  • Fn::FindInMap
  • Ref

使用Join会给您上面发布的稍微有点神秘的错误。但是,因为您可以嵌套FindInMap调用,所以您可以通过创建另一个查找映射来实现地图的“第三维”:

Mappings" : {
  "OrganizationDefaults" : {
        "AZ" : {
            "prod1a" : "subnet-foobar1",
            "qa1a" : "subnet-foobar2",
            "prod1c" : "subnet-foobar3",
            "qa1c" : "subnet-foobar4"
        }
  },
  "OrganizationTwo" : {
        "AZ" : {
            "prod1a" : "subnet-foobar5",
            "qa1a" : "subnet-foobar6",
            "prod1c" : "subnet-foobar7",
            "qa1c" : "subnet-foobar8"
        }
  },
  "EnvMemberMap" : {
        "prod": {
            "1a" : "prod1a",
            "1c" : "prod1c",
        },
        "qa": {
            "1a" : "qa1a",
            "1c" : "qa1c",
        }    
  }
},

然后像这样执行地图检索:

"SubnetId": {
    "Fn::FindInMap": [
        {
            "Ref": "OrganizationName"
        },
        "AZ",
        {
            "Fn::FindInMap": [
                "EnvMemberMap",
                {
                    "Ref": "Environment"
                },
                {
                    "Ref": "Member1AZ"
                }
            ]
        }
    ]
}

答案 1 :(得分:2)

我建议你重构你的映射以避免嵌套的Fn :: Join。

Mappings" : {
      "OrganizationDefaults" : {
            "1a" : {
                "prod" : "subnet-foobar1",
                "qa" : "subnet-foobar2"
            },
            "1c"
                "prod" : "subnet-foobar3",
                "qa" : "subnet-foobar4"
            }
      },
      "OrganizationTwo" : {
            "1a" : {
                "prod" : "subnet-foobar5",
                "qa" : "subnet-foobar6"
            },
            "1c" : {
                "prod" : "subnet-foobar7",
                "qa" : "subnet-foobar8"
            }
      },
},

这简化了您的参考。

"SubnetId" : { "Fn::FindInMap" : [ { "Ref" : "OrganizationName" }, { "Ref" : "Member1AZ" }, { "Ref" : "Environment" }]}