Fn::Select retrieves data elements from a list or dictionary by index or key.
Declaration
Query one or more data elements from a list or an array
Query a single data element based on an index
-
JSON
{ "Fn::Select": [ "index", [ "value1", "value2", ... ], "default_value" ] } -
YAML
-
Syntax for the full function name:
Fn::Select: - index - - value1 - value2 - ... - default_value -
Syntax for the short form:
!Select [index, [value1, value2, ...], default_value]
-
Query multiple data elements based on an index
-
JSON
{ "Fn::Select": [ "start:stop:step", [ "value1", "value2", ... ] ] } -
YAML
-
Syntax for the full function name:
Fn::Select: - start:stop:step - - value1 - value2 - ... -
Syntax for the short form:
!Select [start:stop:step, [value1, value2, ...]]
-
Query the value of a key from a dictionary or mapping table
-
JSON
{ "Fn::Select": [ "key", { "key1": "value1", "key2": "value2", ... }, "default_value" ] } -
YAML
-
Syntax for the full function name:
Fn::Select: - key - key1: value1 key2: value2 ... - default_value -
Syntax for the short form:
!Select [key, {key1:value1, key2:value2}, default_value]
-
Parameters
-
index: The index of the data element to query. Valid values: 0 to N - 1 or -N to -1, where N is the number of elements in the list. Negative values read from right to left. If the index is invalid, the default value is returned. If no default value is specified, an empty string is returned. -
start,stop, andstep: The start and stop positions define the range of elements to query from a list. The step value sets the interval between selected elements.-
start:stop: Valid values follow the same rules as the index parameter. Default value of start: 0. Default value of stop: N. The function returns elements from position start + 1 to position stop. If the values are invalid, an empty list is returned. -
start:stop:step: Default value of step: 1. If step is negative, the start index must be greater than the stop index. The function selects elements at each -(step - 1) interval from start to stop + 1 and returns a list.
-
-
key: A key in the dictionary. The function returns the corresponding value. If the key does not exist, the default value is returned. If no default value is specified, an empty string is returned. -
default_value: The fallback value returned when the specified index or key is invalid. If omitted and the index or key is invalid, an empty string is returned.
Return value
One or more data elements.
Examples
Query one or more data elements from a list or an array
-
!Select ['1', [apples, grapes, oranges, mangoes]]{ "Fn::Select": [ "1", ["apples", "grapes", "oranges", "mangoes"] ] }In this example, "grapes" is returned.
-
!Select ['1:3', [1,2,3,4,5]]{ "Fn::Select": [ "1:3", [1,2,3,4,5] ] }In this example, [2, 3] is returned.
-
!Select ['::2', [1,2,3,4,5]]{ "Fn::Select": [ "::2", [1,2,3,4,5] ] }In this example, [1, 3, 5] is returned.
-
!Select ['5:0:-2', [1,2,3,4,5]]{ "Fn::Select": [ "5:0:-2", [1,2,3,4,5] ] }In this example, [5, 3] is returned.
Query the value of a key from a dictionary
!Select [key1, {key1: grapes, key2: mangoes}]{
"Fn:Select": [
"key1",
{
"key1": "grapes",
"key2": "mangoes"
}
]
}In this example, "grapes" is returned.
Query a data element from a comma-delimited list
Parameters:
userParam:
Type: CommaDelimitedList
Default: 10.0.0.1, 10.0.0.2, 10.0.0.3
Resources:
resourceID:
Properties:
CidrBlock:
!Select:
- '0'
- !Ref userParam
In this example, 10.0.0.1 is returned.
Supported functions
The Fn::Select index value supports the Ref function.
The Fn::Select list of objects supports the following functions: