Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates AWS Cloudformation:Fn::在Fn::FindInMap语句中联接?_Templates_Amazon Web Services_Amazon Cloudformation - Fatal编程技术网

Templates AWS Cloudformation:Fn::在Fn::FindInMap语句中联接?

Templates AWS Cloudformation:Fn::在Fn::FindInMap语句中联接?,templates,amazon-web-services,amazon-cloudformation,Templates,Amazon Web Services,Amazon Cloudformation,正在尝试在Fn::FindInMap中使用Fn::Join,如下所示: "SubnetId": { "Fn::FindInMap": [ { "Ref": "OrganizationName" }, "AZ", { "Fn::Join": [ "", [ {

正在尝试在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"
            }
      },
},

有人能帮上忙吗,或者以前做过类似的事情吗?对于列出的任何组织,我都需要使用相同的模板,因此映射应该可以为我解决这个问题,如果我能正确地做到这一点。

我建议您重构映射,以避免嵌套的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" }]}

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

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

  • 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"
                }
            ]
        }
    ]
}

同意,这意味着如果需要为映射执行字符串concat,则可以将其拆分为多个映射。