github.com/pulumi/terraform@v1.4.0/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  This function maps
    12  [Terraform language values](/language/expressions/types)
    13  to YAML tags in the following way:
    14  
    15  | Terraform type | YAML type            |
    16  | -------------- | -------------------- |
    17  | `string`       | `!!str`              |
    18  | `number`       | `!!float` or `!!int` |
    19  | `bool`         | `!!bool`             |
    20  | `list(...)`    | `!!seq`              |
    21  | `set(...)`     | `!!seq`              |
    22  | `tuple(...)`   | `!!seq`              |
    23  | `map(...)`     | `!!map`              |
    24  | `object(...)`  | `!!map`              |
    25  | Null value     | `!!null`             |
    26  
    27  `yamlencode` uses the implied syntaxes for all of the above types, so it does
    28  not generate explicit YAML tags.
    29  
    30  Because the YAML format cannot fully represent all of the Terraform language
    31  types, passing the `yamlencode` result to `yamldecode` will not produce an
    32  identical value, but the Terraform language automatic type conversion rules
    33  mean that this is rarely a problem in practice.
    34  
    35  YAML is a superset of JSON, and so where possible we recommend generating
    36  JSON using [`jsonencode`](/language/functions/jsonencode) instead, even if
    37  a remote system supports YAML. JSON syntax is equivalent to flow-style YAML
    38  and Terraform can present detailed structural change information for JSON
    39  values in plans, whereas Terraform will treat block-style YAML just as a normal
    40  multi-line string. However, generating YAML may improve readability if the
    41  resulting value will be directly read or modified in the remote system by
    42  humans.
    43  
    44  ## Examples
    45  
    46  ```
    47  > yamlencode({"a":"b", "c":"d"})
    48  "a": "b"
    49  "c": "d"
    50  
    51  > yamlencode({"foo":[1, 2, 3], "bar": "baz"})
    52  "bar": "baz"
    53  "foo":
    54  - 1
    55  - 2
    56  - 3
    57  
    58  > yamlencode({"foo":[1, {"a":"b","c":"d"}, 3], "bar": "baz"})
    59  "bar": "baz"
    60  "foo":
    61  - 1
    62  - "a": "b"
    63    "c": "d"
    64  - 3
    65  ```
    66  
    67  `yamlencode` always uses YAML's "block style" for mappings and sequences, unless
    68  the mapping or sequence is empty. To generate flow-style YAML, use
    69  [`jsonencode`](/language/functions/jsonencode) instead: YAML flow-style is a superset
    70  of JSON syntax.
    71  
    72  ## Related Functions
    73  
    74  - [`jsonencode`](/language/functions/jsonencode) is a similar operation using JSON instead
    75    of YAML.
    76  - [`yamldecode`](/language/functions/yamldecode) performs the opposite operation, _decoding_
    77    a YAML string to obtain its represented value.