简体   繁体   English

CloudFormation — 可以嵌套映射吗?

[英]CloudFormation — possible to have nested Mappings?

Is it possible to have nested Mappings in CloudFormation, like the following example?是否可以在 CloudFormation 中嵌套映射,如下例所示?

"Mappings" :  
{
    "Regions" : 
    {
        "us-east-1" : 
        {
            "Environments" :
            {
                "dev" : 
                {
                    "ImageId" : "something",
                    "Subnet" : "something"
                },
                "qa" :
                {
                    "ImageId" : "something",
                    "Subnet" : "something"
                }
            }
        },
        "us-west-2" : 
        {
            "Environments" :
            {
                "dev" : 
                {
                    "ImageId" : "something",
                    "Subnet" : "something"
                },
                "qa" :
                {
                    "ImageId" : "something",
                    "Subnet" : "something"
                }
            }
        }
    }
}

When I attempt to do something like this, I get the following error:当我尝试做这样的事情时,我收到以下错误:

Template format error: Every Mappings attribute must be a String or a List.模板格式错误:每个映射属性都必须是字符串或列表。

If nested Mappings aren't possible, then what is the best way to store values in a CFT that require two parameters to select (such as values that depend on BOTH Region and environment)?如果嵌套映射是不可能的,那么在需要选择两个参数的 CFT 中存储值的最佳方法是什么(例如取决于两个区域和环境的值)?

I ended up doing it like this:我最终这样做了:

"Mappings" :  
{
    "dev" : 
    {
        "us-east-1" :
        {
            "ImageId" : "something",
            "Subnet" : "something"
        },
        "us-west-2" :
        {
            "ImageId" : "something",
            "Subnet" : "something"
        }
    },
    "qa" : 
    {
        "us-east-1" :
        {
            "ImageId" : "something",
            "Subnet" : "something"
        },
        "us-west-2" :
        {
            "ImageId" : "something",
            "Subnet" : "something"
        }
    }
}

The important point here is that the objects alternate between "mappings" and keys". So in this situation, "dev" is a Mapping, "us-east-1" is a key, "ImageId" is a mapping, and "something" is a key. Mapping names cannot have non-alphanumeric characters, so Region names cannot be Mappings. Thus, using the environment as the first parameter and using the region name as the second parameter is mandatory.这里的重点是对象在“映射”和“键”之间交替。所以在这种情况下,“dev”是一个映射,“us-east-1”是一个键,“ImageId”是一个映射,而“东西" 是一个键。映射名称不能有非字母数字字符,所以区域名称不能是映射。因此,使用环境作为第一个参数,使用区域名称作为第二个参数是强制性的。

It seems to me like the Mappings section of CloudFormation has a lot of really strange arbitrary rules, and it surprises me that it isn't more flexible, but there you have it.在我看来,CloudFormation 的 Mappings 部分有很多非常奇怪的任意规则,令我惊讶的是它并没有更灵活,但你有它。

Faced the same issue.面临同样的问题。 Found the following in the Fn::FindInMap documentationFn::FindInMap 文档中找到以下内容

The intrinsic function Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section.内部函数 Fn::FindInMap 返回与在 Mappings 部分声明的两级映射中的键对应的值。

So basically:所以基本上:

The following will work:以下将起作用:

Mappings:
    map_name:
        level_1_key:
            level_2_key:
                "value"

But 3 levels won't.但是3级不会。

Mappings:
    map_name:
        level_1_key:
            level_2_key:
                level_3_key:
                    "value"

Something like this:像这样的东西:

"ImageMap" : {
    "us-east-1" : { "dev" : "ami-11111111", "qa" : "ami-22222222" },
    "us-west-1" : { "dev" : "ami-33333333", "qa" : "ami-44444444" }
 }

and then to access these values:然后访问这些值:

"Value" : {
    "Fn::FindInMap" : [
        "ImageMap", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM