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

     1  ---
     2  page_title: yamlencode - Functions - Configuration Language
     3  description: The yamlencode function encodes a given value as a YAML string.
     4  ---
     5  
     6  # `yamlencode` Function
     7  
     8  `yamlencode` encodes a given value to a string using
     9  [YAML 1.2](https://yaml.org/spec/1.2/spec.html) block syntax.
    10  
    11  ~> **Warning:** This function is currently **experimental** and its exact
    12  result format may change in future versions of Terraform, based on feedback.
    13  Do not use `yamldecode` to construct a value for any resource argument where
    14  changes to the result would be disruptive. To get a consistent string
    15  representation of a value use [`jsonencode`](/language/functions/jsonencode) instead; its
    16  results are also valid YAML because YAML is a JSON superset.
    17  
    18  <!--
    19      The condition for removing the above warning is that the underlying
    20      go-cty-yaml module makes a stable release with a commitment to guarantee
    21      that the representation of particular input will not change without a
    22      major release. It is not making that commitment at the time of writing to
    23      allow for responding to user feedback about its output format, since YAML
    24      is a very flexible format and its initial decisions may prove to be
    25      sub-optimal when generating YAML intended for specific external consumers.
    26  -->
    27  
    28  This function maps
    29  [Terraform language values](/language/expressions/types)
    30  to YAML tags in the following way:
    31  
    32  | Terraform type | YAML type            |
    33  | -------------- | -------------------- |
    34  | `string`       | `!!str`              |
    35  | `number`       | `!!float` or `!!int` |
    36  | `bool`         | `!!bool`             |
    37  | `list(...)`    | `!!seq`              |
    38  | `set(...)`     | `!!seq`              |
    39  | `tuple(...)`   | `!!seq`              |
    40  | `map(...)`     | `!!map`              |
    41  | `object(...)`  | `!!map`              |
    42  | Null value     | `!!null`             |
    43  
    44  `yamlencode` uses the implied syntaxes for all of the above types, so it does
    45  not generate explicit YAML tags.
    46  
    47  Because the YAML format cannot fully represent all of the Terraform language
    48  types, passing the `yamlencode` result to `yamldecode` will not produce an
    49  identical value, but the Terraform language automatic type conversion rules
    50  mean that this is rarely a problem in practice.
    51  
    52  ## Examples
    53  
    54  ```
    55  > yamlencode({"a":"b", "c":"d"})
    56  "a": "b"
    57  "c": "d"
    58  
    59  > yamlencode({"foo":[1, 2, 3], "bar": "baz"})
    60  "bar": "baz"
    61  "foo":
    62  - 1
    63  - 2
    64  - 3
    65  
    66  > yamlencode({"foo":[1, {"a":"b","c":"d"}, 3], "bar": "baz"})
    67  "bar": "baz"
    68  "foo":
    69  - 1
    70  - "a": "b"
    71    "c": "d"
    72  - 3
    73  ```
    74  
    75  `yamlencode` always uses YAML's "block style" for mappings and sequences, unless
    76  the mapping or sequence is empty. To generate flow-style YAML, use
    77  [`jsonencode`](/language/functions/jsonencode) instead: YAML flow-style is a superset
    78  of JSON syntax.
    79  
    80  ## Related Functions
    81  
    82  - [`jsonencode`](/language/functions/jsonencode) is a similar operation using JSON instead
    83    of YAML.
    84  - [`yamldecode`](/language/functions/yamldecode) performs the opposite operation, _decoding_
    85    a YAML string to obtain its represented value.