CloudFormation — possible to have nested Mappings?

被刻印的时光 ゝ 提交于 2019-12-06 02:31:24

问题


Is it possible to have nested Mappings in CloudFormation, like the following example?

"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)?


回答1:


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



回答2:


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.

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.



来源:https://stackoverflow.com/questions/44027960/cloudformation-possible-to-have-nested-mappings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!