website: Recommend against making JSON/YAML with heredocs

This is a repeated cause of confusion and questions in the community
forum, because both JSON and YAML valid syntax are hard to generate using
just string concatenation. Terraform has built-in functions for both of
these common serializations to avoid those problems, and so this will
hopefully make these better alternatives more discoverable.
This commit is contained in:
Martin Atkins 2020-12-07 15:25:57 -08:00
parent ab1dd87540
commit 776b33db32
1 changed files with 14 additions and 0 deletions

View File

@ -72,6 +72,20 @@ In the above example, `EOT` is the identifier selected. Any identifier is
allowed, but conventionally this identifier is in all-uppercase and begins with
`EO`, meaning "end of". `EOT` in this case stands for "end of text".
### Generating JSON or YAML
Don't use "heredoc" strings to generate JSON or YAML. Instead, use
[the `jsonencode` function](../functions/jsonencode.html) or
[the `yamlencode` function](../functions/yamlencode.html) so that Terraform
can be responsible for guaranteeing valid JSON or YAML syntax.
```hcl
example = jsonencode({
a = 1
b = "hello"
})
```
### Indented Heredocs
The standard heredoc form (shown above) treats all space characters as literal