github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/element.mdx (about)

     1  ---
     2  page_title: element - Functions - Configuration Language
     3  description: The element function retrieves a single element from a list.
     4  ---
     5  
     6  # `element` Function
     7  
     8  `element` retrieves a single element from a list.
     9  
    10  ```hcl
    11  element(list, index)
    12  ```
    13  
    14  The index is zero-based. This function produces an error if used with an
    15  empty list. The index must be a non-negative integer.
    16  
    17  Use the built-in index syntax `list[index]` in most cases. Use this function
    18  only for the special additional "wrap-around" behavior described below.
    19  
    20  ## Examples
    21  
    22  ```
    23  > element(["a", "b", "c"], 1)
    24  b
    25  ```
    26  
    27  If the given index is greater than the length of the list then the index is
    28  "wrapped around" by taking the index modulo the length of the list:
    29  
    30  ```
    31  > element(["a", "b", "c"], 3)
    32  a
    33  ```
    34  
    35  To get the last element from the list use [`length`](/language/functions/length) to find
    36  the size of the list (minus 1 as the list is zero-based) and then pick the
    37  last element:
    38  
    39  ```
    40  > element(["a", "b", "c"], length(["a", "b", "c"])-1)
    41  c
    42  ```
    43  
    44  ## Related Functions
    45  
    46  * [`index`](/language/functions/index_function) finds the index for a particular element value.
    47  * [`lookup`](/language/functions/lookup) retrieves a value from a _map_ given its _key_.