github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/functions/yamlencode.html.md (about)

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