All Products
Search
Document Center

Resource Orchestration Service:Fn::FindInMap

Last Updated:Mar 15, 2024

The built-in function 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 that is assigned to the SecondLevelKey parameter.

Examples

For example, you want to specify the ImageId property when you create a resource named WebServer. In this case, you can use regionParam in the Parameters section to specify regions, and use RegionMap in the Mappings section to specify the ImageId mapping in each region. This way, Fn::FindInMap queries the specified ImageId mapping in RegionMap based on the specified region and queries the required ImageId property in the mapping.

  • The MapName parameter is set to RegionMap.

  • The TopLevelKey parameter 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 SecondLevelKey parameter 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"
          }
        ]
      }
    }
  }
}

Supported functions