AWS Cloudformation:Fn :: Fin :: FindInMap语句中的联接?

ste*_*zee 6 templates amazon-web-services aws-cloudformation

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

"SubnetId": {
    "Fn::FindInMap": [
        {
            "Ref": "OrganizationName"
        },
        "AZ",
        {
            "Fn::Join": [
                "",
                [
                    {
                        "Ref": "Environment"
                    },
                    {
                        "Ref": "Member1AZ"
                    }
                ]
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

"SubnetId" : { "Fn::FindInMap" : [ "Organization2", "AZ", "prod1c" ]}
Run Code Online (Sandbox Code Playgroud)

但是,它似乎没有将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
Run Code Online (Sandbox Code Playgroud)

我的映射如下:

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"
            }
      },
},
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供帮助,或者之前必须做类似的事情?我需要为列出的任何组织使用相同的模板,因此Mappings应该为我解决此问题,如果我能正确解决的话。

Lee*_*ton 5

尽管我同意@Jason的观点,对于您来说,重构地图布局是最适合您的解决方案,但是在某些情况下,CloudFormation中地图的2D限制可能会受到限制,因此我将在此处发布一种可能的解决方案。

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

  • Fn::FindInMap
  • Ref

使用a 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",
        }    
  }
},
Run Code Online (Sandbox Code Playgroud)

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

"SubnetId": {
    "Fn::FindInMap": [
        {
            "Ref": "OrganizationName"
        },
        "AZ",
        {
            "Fn::FindInMap": [
                "EnvMemberMap",
                {
                    "Ref": "Environment"
                },
                {
                    "Ref": "Member1AZ"
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*son 4

我建议您重构映射以避免嵌套 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"
            }
      },
},
Run Code Online (Sandbox Code Playgroud)

这简化了您的参考。

"SubnetId" : { "Fn::FindInMap" : [ { "Ref" : "OrganizationName" }, { "Ref" : "Member1AZ" }, { "Ref" : "Environment" }]}
Run Code Online (Sandbox Code Playgroud)