Fn::FindInMap returns the value that corresponds to keys in a two-level mapping declared in the Mappings section.
Declaration
-
JSON
{ "Fn::FindInMap": [ "MapName", "TopLevelKey", "SecondLevelKey" ] } -
YAML
-
Syntax for the full function name:
Fn::FindInMap: - MapName - TopLevelKey - SecondLevelKey -
Syntax for the short form:
!FindInMap [MapName,TopLevelKey,SecondLevelKey]
-
Parameters
-
MapName: the name of a mapping declared in the Mappings section. For more information, see Mappings. -
TopLevelKey: the top-level key. The value consists of key-value pairs. -
SecondLevelKey: the second-level key. The value is a string or a number.
Return value
The value assigned to SecondLevelKey.
Examples
The following example specifies the ImageId property for a resource named WebServer. It uses regionParam in the Parameters section to specify regions and RegionMap in the Mappings section to map each region to an ImageId. Fn::FindInMap looks up the ImageId in RegionMap based on the specified region.
-
The
MapNameparameter is set toRegionMap. -
The
TopLevelKeyparameter is set to the region where the stack is created. In this example, the parameter is determined by using! Ref regionParam. For more information, see Ref. -
The
SecondLevelKeyparameter is set to the desired architecture. In this example,"32"is used.
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
regionParam:
Description: the region where you want to create the ECS instance
Type: String
AllowedValues:
- hangzhou
- beijing
Mappings:
RegionMap:
hangzhou:
'32': m-25l0rcfjo
'64': m-25l0rcfj1
beijing:
'32': m-25l0rcfj2
'64': m-25l0rcfj3
Resources:
WebServer:
Type: ALIYUN::ECS::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref regionParam, "32"]
InstanceType: ecs.t1.small
SecurityGroupId: sg-25zwc****
ZoneId: cn-beijing-b
Tags:
- Key: key1
Value: value1
- Key: key2
Value: value2
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"regionParam": {
"Description": "the region where you want to create the ECS instance",
"Type": "String",
"AllowedValues": [
"hangzhou",
"beijing"
]
}
},
"Mappings": {
"RegionMap": {
"hangzhou": {
"32": "m-25l0rcfjo",
"64": "m-25l0rcfj1"
},
"beijing": {
"32": "m-25l0rcfj2",
"64": "m-25l0rcfj3"
}
}
},
"Resources": {
"WebServer": {
"Type": "ALIYUN::ECS::Instance",
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionMap",
{
"Ref": "regionParam"
},
"32"
]
},
"InstanceType": "ecs.t1.small",
"SecurityGroupId": "sg-25zwc****",
"ZoneId": "cn-beijing-b",
"Tags": [
{
"Key": "key1",
"Value": "value1"
},
{
"Key": "key2",
"Value": "value2"
}
]
}
}
}
}