ROS templates support querying values from maps stored as key-value pairs. Depending on whether the map is defined in the Mappings section and how many levels it has, use the intrinsic functions described in this topic.
Choose the right function based on your map type:
|
Map type |
Defined in Mappings section |
Recommended function |
|
Single-level |
No |
|
|
Single-level (JSON string) |
No |
|
|
Two-level |
Yes |
|
|
Two-level |
No |
Nested |
|
Multi-level |
Yes (two-level + deeper) |
|
|
Multi-level |
No |
Nested |
|
Multi-level (complex filters) |
Either |
|
Query a value from a single-level map
Do not define a single-level map in the Mappings section. Resource Orchestration Service (ROS) cannot query a value from a single-level map in the Mappings section. For more information, see Mappings.
Fn::Select
Use Fn::Select to query a value from a single-level map.
Fn::Select takes a key and a map object, and returns the matching value. For more information, see Fn::Select.
-
Query
value_afrom a JSON object parameter — givenkey_a, returnsvalue_a:ROSTemplateFormatVersion: '2015-09-01' Parameters: InputMap: Type: Json Outputs: value_a: Value: Fn::Select: - key_a - InputMap -
Query a value from an inline map defined directly in the template:
ROSTemplateFormatVersion: '2015-09-01' Resources: Mock: Type: MockResource Properties: Prop1: Fn::Select: - key - key1: value1 key2: value2
Fn::GetJsonValue
When the map is passed as a JSON string rather than a JSON object, use Fn::GetJsonValue to query the value at a first-level key. For more information, see Fn::GetJsonValue.
Given key_a, returns value_a:
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
Fn::FindInMap
Fn::FindInMap only works with two-level maps defined in the Mappings section.
Use Fn::FindInMap to look up a value by top-level key and second-level key. For more information, see Mappings and Fn::FindInMap.
The following example selects an ECS image ID based on region and architecture. Given regionParam=hangzhou and architecture '32', returns m-25l0rcfjo:
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
Use nested Fn::Select for two-level maps that are not in the Mappings section.
Nest Fn::Select twice to drill into a two-level map. The inner call retrieves the sub-map at key_1, and the outer call retrieves the value at key_1_1. For more information, see Fn::Select.
Returns value_1_1:
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
Query a value from a multi-level map
Fn::Select
-
For maps with more than two levels in the Mappings section, combine
Fn::FindInMapandFn::Select: useFn::FindInMapto retrieve a sub-map at the two-level boundary, then useFn::Selectto access the deeper key.Fn::Select: - key - Fn::FindInMap: - MapName - TopLevelKey - SecondLevelKey For multi-level maps outside the Mappings section, nest
Fn::Selectmultiple times using the same pattern as the two-level example. For more information, see Fn::Select.
Fn::Jq
Use Fn::Jq when you need complex filtering across nested structures — for example, extracting specific fields from an array of objects. For more information, see Fn::Jq.
The following example extracts name and type from each object in the parameters array:
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
Returns:
- param_name: PKG_TAG_NAME
- param_name: GIT_COMMIT
- param_name: TRIGGERED_JOB