OpenTofu® builds text with string templates: ${…} puts a value into text, and %{…} directives repeat or choose parts of it. A heredoc lets that text span several lines. This guide builds a greeting, a title and a shopping list.
Syntax
"text ${<EXPRESSION>} text"
"text %{if <CONDITION>}shown when true%{else}shown when false%{endif}"
<<EOT
%{for <ITEM> in <COLLECTION>~}
text with ${<ITEM>}
%{endfor~}
EOT
${<EXPRESSION>}: an interpolation. OpenTofu replaces it with the value, turned into text.%{if <CONDITION>}…%{else}…%{endif}: keeps one part or the other.%{else}is optional.%{for <ITEM> in <COLLECTION>}…%{endfor}: repeats the text between them once for each item.~: a strip marker. Inside the braces, it removes the spaces and line break next to that side.<<EOT…EOT: a heredoc. The text runs from the next line to a line holding only the word you chose.<<-EOTalso removes the shared indentation.
The full rules are in Strings and Templates (opens in a new tab) in the OpenTofu docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Write the Templates
In an empty folder, create main.tf:
variable "name" {
type = string
default = "Ana"
}
variable "items" {
type = list(string)
default = ["tea", "bread", "milk"]
}
variable "urgent" {
type = bool
default = true
}
output "greeting" {
value = "Hello, ${var.name}!"
}
output "title" {
value = "Shopping list%{if var.urgent} (urgent)%{endif}"
}
output "shopping_list" {
value = <<EOT
Shopping list for ${var.name}:
%{for item in var.items~}
- ${item}
%{endfor~}
EOT
}
output "note" {
value = <<-EOT
Pick up the order at 5 pm.
Pay at the counter.
EOT
}
output "literal" {
value = "Write $${var.name} to insert a variable."
}
greetingputsvar.nameinto a sentence.titleadds “(urgent)” only whenvar.urgentis true.shopping_listis a heredoc whose%{for}repeats one line per item. The~stops each directive from leaving an empty line.noteis an indented heredoc,<<-EOT, so its lines can follow the indentation of the file.literalwrites$${to print${as it is.
Step 2: Apply It
This configuration uses no providers, so there’s nothing for tofu init to download: tofu apply works straight away. Run it and type yes:
tofu apply
Outputs:
greeting = "Hello, Ana!"
literal = "Write ${var.name} to insert a variable."
note = <<EOT
Pick up the order at 5 pm.
Pay at the counter.
EOT
shopping_list = <<EOT
Shopping list for Ana:
- tea
- bread
- milk
EOT
title = "Shopping list (urgent)"
OpenTofu prints multi-line text as a heredoc. The empty line before EOT is the line break that ends the last line.
Two Things to Know
tofu fmtremoves the spaces inside directives. The docs write%{ for item in var.items ~};tofu fmtrewrites it as%{for item in var.items~}. Both work.- Don’t combine an indented heredoc with strip markers that remove line breaks. The lines after them keep their spaces, as Fixed: OpenTofu indented heredoc not trimming spaces shows. Write such a template flush left with
<<EOT, asshopping_listdoes.
Clean Up
This example created nothing but a state file. Delete the folder when you’re done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Using a heredoc to write JSON or YAML | A missing comma or quote breaks the file. | The docs say to use jsonencode or yamlencode instead. |
Writing ${ in text that should keep it | OpenTofu tries to evaluate it. | Write $${, or %%{ for %{. |
FAQ
Do escape sequences like \n work in a heredoc?
No. The docs say backslashes are literal in a heredoc; only $${ and %%{ are special. In a quoted string, \n is a line break.
Can I put a template in a separate file?
Yes, with the templatefile function, which reads a file and fills in the same ${…} and %{…} sequences.
Conclusion
${…} inserts values, %{if} and %{for} choose and repeat text, and a heredoc spreads it over several lines. Use strip markers to control the line breaks, with the heredoc flush left.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- How to Use Conditional Expressions in OpenTofu
- How to Use for Expressions in OpenTofu
- Fixed: OpenTofu Indented Heredoc Not Trimming Spaces After a Strip Marker
Additional Resources
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.
0 comments