Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFormation -- possible to have nested Mappings?

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

like image 422
jcgrowley Avatar asked May 17 '17 14:05

jcgrowley


2 Answers

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.

like image 139
jcgrowley Avatar answered Sep 24 '22 16:09

jcgrowley


Faced the same issue. Found the following in the Fn::FindInMap documentation

The intrinsic function Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section.

So basically:

The following will work:

Mappings:
    map_name:
        level_1_key:
            level_2_key:
                "value"

But 3 levels won't.

Mappings:
    map_name:
        level_1_key:
            level_2_key:
                level_3_key:
                    "value"
like image 42
akshaynagpal Avatar answered Sep 25 '22 16:09

akshaynagpal