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

     1  ---
     2  page_title: yamldecode - Functions - Configuration Language
     3  description: |-
     4    The yamldecode function decodes a YAML string into a representation of its
     5    value.
     6  ---
     7  
     8  # `yamldecode` Function
     9  
    10  `yamldecode` parses a string as a subset of YAML, and produces a representation
    11  of its value.
    12  
    13  This function supports a subset of [YAML 1.2](https://yaml.org/spec/1.2/spec.html),
    14  as described below.
    15  
    16  This function maps YAML values to
    17  [Terraform language values](/language/expressions/types)
    18  in the following way:
    19  
    20  | YAML type     | Terraform type                                                     |
    21  | ------------- | ------------------------------------------------------------------ |
    22  | `!!str`       | `string`                                                           |
    23  | `!!float`     | `number`                                                           |
    24  | `!!int`       | `number`                                                           |
    25  | `!!bool`      | `bool`                                                             |
    26  | `!!map`       | `object(...)` with attribute types determined per this table       |
    27  | `!!seq`       | `tuple(...)` with element types determined per this table          |
    28  | `!!null`      | The Terraform language `null` value                                |
    29  | `!!timestamp` | `string` in [RFC 3339](https://tools.ietf.org/html/rfc3339) format |
    30  | `!!binary`    | `string` containing base64-encoded representation                  |
    31  
    32  The Terraform language automatic type conversion rules mean that you don't
    33  usually need to worry about exactly what type is produced for a given value,
    34  and can just use the result in an intuitive way.
    35  
    36  Note though that the mapping above is ambiguous -- several different source
    37  types map to the same target type -- and so round-tripping through `yamldecode`
    38  and then `yamlencode` cannot produce an identical result.
    39  
    40  YAML is a complex language and it supports a number of possibilities that the
    41  Terraform language's type system cannot represent. Therefore this YAML decoder
    42  supports only a subset of YAML 1.2, with restrictions including the following:
    43  
    44  - Although aliases to earlier anchors are supported, cyclic data structures
    45    (where a reference to a collection appears inside that collection) are not.
    46    If `yamldecode` detects such a structure then it will return an error.
    47  
    48  - Only the type tags shown in the above table (or equivalent alternative
    49    representations of those same tags) are supported. Any other tags will
    50    result in an error.
    51  
    52  - Only one YAML document is permitted. If multiple documents are present in
    53    the given string then this function will return an error.
    54  
    55  ## Examples
    56  
    57  ```
    58  > yamldecode("hello: world")
    59  {
    60    "hello" = "world"
    61  }
    62  
    63  > yamldecode("true")
    64  true
    65  
    66  > yamldecode("{a: &foo [1, 2, 3], b: *foo}")
    67  {
    68    "a" = [
    69      1,
    70      2,
    71      3,
    72    ]
    73    "b" = [
    74      1,
    75      2,
    76      3,
    77    ]
    78  }
    79  
    80  > yamldecode("{a: &foo [1, *foo, 3]}")
    81  
    82  Error: Error in function call
    83  
    84  Call to function "yamldecode" failed: cannot refer to anchor "foo" from inside
    85  its own definition.
    86  
    87  > yamldecode("{a: !not-supported foo}")
    88  
    89  Error: Error in function call
    90  
    91  Call to function "yamldecode" failed: unsupported tag "!not-supported".
    92  ```
    93  
    94  ## Related Functions
    95  
    96  - [`jsondecode`](/language/functions/jsondecode) is a similar operation using JSON instead
    97    of YAML.
    98  - [`yamlencode`](/language/functions/yamlencode) performs the opposite operation, _encoding_
    99    a value as YAML.