La fonction Fn::FindInMap renvoie la valeur correspondant aux clés d’un mappage à deux niveaux déclaré dans la section Mappings.
Déclaration
-
JSON
{ "Fn::FindInMap": [ "MapName", "TopLevelKey", "SecondLevelKey" ] } -
YAML
-
Syntaxe du nom complet de la fonction :
Fn::FindInMap: - MapName - TopLevelKey - SecondLevelKey -
Syntaxe de la forme abrégée :
!FindInMap [MapName,TopLevelKey,SecondLevelKey]
-
Paramètres
MapName: nom du mappage déclaré dans la section Mappings. Pour plus d’informations, consultez la rubrique Mappings.TopLevelKey: clé de premier niveau. La valeur se compose de paires clé-valeur.SecondLevelKey: clé de deuxième niveau. La valeur est une chaîne ou un nombre.
Valeur de retour
Valeur attribuée à SecondLevelKey.
Exemples
L’exemple suivant spécifie la propriété ImageId pour une ressource nommée WebServer. Il utilise le paramètre regionParam dans la section Parameters pour définir les régions, et RegionMap dans la section Mappings pour associer chaque région à un ImageId. La fonction Fn::FindInMap recherche l’ImageId dans RegionMap en fonction de la région spécifiée.
Le paramètre
MapNameest défini surRegionMap.Le paramètre
TopLevelKeycorrespond à la région où la pile est créée. Dans cet exemple, ce paramètre est déterminé à l’aide de! Ref regionParam. Pour plus d’informations, consultez la rubrique Ref.Le paramètre
SecondLevelKeycorrespond à l’architecture souhaitée. Dans cet exemple, la valeur"32"est utilisée.
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"
}
]
}
}
}
}