【问题标题】:AWS Cloudformation : Fn::Join within a Fn::FindInMap statement?AWS Cloudformation : Fn::Join 在 Fn::FindInMap 语句中?
【发布时间】:2015-04-28 21:32:08
【问题描述】:

尝试在 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 应该为我解决这个问题,如果我能做对的话。

【问题讨论】:

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


    【解决方案1】:

    虽然我同意@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"
                    }
                ]
            }
        ]
    }
    

    【讨论】:

      【解决方案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" }]}
      

      【讨论】:

      • 同意,这意味着如果您需要对映射进行字符串连接,您可以随时将其拆分为多个映射。
      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2015-02-03
      相关资源
      最近更新 更多