All Products
Search
Document Center

Resource Orchestration Service:Fn::Select

Last Updated:Mar 15, 2024

The built-in function Fn::Select returns data elements from a list or a dictionary based on an index.

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 parameter specifies the index of the data element that you want to query. The index is an integer that ranges from 0 to N - 1 or from -N to -1. N specifies the number of data elements in the list. The negative sign indicates that the data elements are read from right to left. If you specify an invalid index and specify a default value for the index, the default value is returned. If you specify an invalid index but do not specify a default value for the index, an empty string is returned.

  • start, stop, and step: The start and stop parameters specify the start and stop positions based on which the function queries data elements from a list. The step parameter specifies the step size. The function queries a data element at each step - 1 interval and returns a list of the data elements.

    • start:stop: The requirements on valid values of the start, stop, and index parameters are the same. If you leave the start parameter empty, the default value of the start parameter is 0. If you leave the stop parameter empty, the default value of the stop parameter is N. The function queries data elements from the start + 1 position to the stop position in the list and returns a list of the data elements. If the values of the start and stop parameters are invalid, an empty list is returned.

    • start:stop:step: If you leave the step parameter empty, the default value is 1. If you set the step parameter to a negative number, the index of the data element at the start position must be greater than the index of the data element at the stop position. The function queries a data element at each -(step - 1) interval from the start position to the stop + 1 position and returns a list of the data elements.

  • key: The parameter specifies a key in the dictionary. The function returns the value of the key. If you specify a key that is not contained in the dictionary and specify a default value for the key, the default value is returned. If you specify a key that is not contained in the dictionary but do not specify a default value for the key, an empty string is returned.

  • default_value: The parameter specifies the default value for the index or key parameter when the index or key that you specify is invalid. If you leave the default_value parameter empty when the index or key that you specify is invalid, an empty string is returned.

Return value

The 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

For the Fn::Select index value, you can use the Ref function.

For the Fn::Select list of objects, you can use the following functions: