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