All Products
Search
Document Center

Resource Orchestration Service:How to query a value from a map in a template?

Last Updated:Mar 20, 2024

The map data structure stores data as a collection of key-value pairs. This topic describes how to query a value from a map that is defined in a template.

Query a value from a single-level map

This section describes the functions that you can invoke to query a value from a single-level map.

Note

We recommend that you do not define a single-level map in the Mappings section of a template. Resource Orchestration Service (ROS) cannot query a value from a single-level map in the Mappings section. For more information, see Mappings.

Fn::Select

Note

We recommend that you use this function to query a value from a single-level map.

  • When you specify a JSON object, you can invoke the Fn::Select function to query value_a that corresponds to key_a. For more information, see Fn::Select.

    ROSTemplateFormatVersion: '2015-09-01'
    Parameters:
    	InputMap:
      	Type: Json  
    Outputs:
    	value_a:
      	Value:
        	Fn::Select:
        	- key_a
        	- InputMap
  • You can also invoke this function to query a value from a dictionary.

    ROSTemplateFormatVersion: '2015-09-01'
    Resources:
      Mock:
        Type: MockResource
        Properties:
          Prop1:
            Fn::Select:
            - key
            - key1: value1
              key2: value2

Fn::GetJsonValue

When you specify a JSON string, you can invoke the Fn::GetJsonValue function to query the value that corresponds to the first-layer key. For more information, see Fn::GetJsonValue.

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
	InputMapJsonString:
  	Type: Json
Outputs:
	value_a:
  	Value:
    	Fn::GetJsonValue:
    	- key_a
    	- InputMapJsonString

Query a value from a two-level map

This section describes the functions that you can invoke to query a value from a two-level map.

Fn::FindInMap

Note

This function is suitable only for querying a value from a two-level map that is defined in the Mappings section.

You can invoke the Fn::FindInMap function to query a value from a two-level map that is defined in the Mappings section of a template. For more information, see Mappings and Fn::FindInMap.

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  regionParam:
    Description: the region where you want to create the Elastic Compute Service (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'

Fn::Select

Note

This function is suitable for querying a value from a two-level map that is not defined in the Mappings section.

You can nest the Fn::Select function to query a value from a two-level map. For more information, see Fn::Select.

Fn::Select:
- key_1_1:
- Fn::Select:
	- key_1
	- key_1:
    	key_1_1: value_1_1
    	key_1_2: value_1_2
    key_2:
      key_2_1: value_2_1
      key_2_2: value_2_2

In this example: value_1_1 is returned.

Query a value from a multi-level map

This section describes the functions that you can invoke to query a value from a multi-level map.

Fn::Select

  • You can nest the Fn::Select and Fn::FindInMap functions to query a value from a multi-level map that is defined in the Mappings section. ROS allows you to use only the Fn::FindInMap function to query a value in the Mappings section. If you want to query a value from a map that has more than two levels in the Mappings section, you can use the Fn::FindInMap function to query a map collection from a two-level map. Then, you can use the Fn::Select function to query the desired value.

     Fn::Select:
       - key 
       - Fn::FindInMap:
            - MapName
            - TopLevelKey
            - SecondLevelKey
  • You can also nest the Fn::Select function multiple times to query a value from a multi-level map that is not defined in the Mappings section. For more information, see Fn::Select.

Fn::Jq

You can use this function to specify multiple filters to meet your business requirements on queries of multi-level maps. For more information, see Fn::Jq.

Fn::Jq:
  - All
  - '.parameters[] | {"param_name": .name, "param_type":.type}'
  - changeSet:
      items: []
      kind: git
    id: 2013-12-27_00-09-37
    parameters:
      - name: PKG_TAG_NAME
        value: trunk
      - name: GIT_COMMIT
        value: master
      - name: TRIGGERED_JOB
        value: trunk-buildall

In this example, the following result is returned:

- param_name: PKG_TAG_NAME
- param_name: GIT_COMMIT
- param_name: TRIGGERED_JOB