Skip to main content
0 reactions

OpenTofu String Templates and Heredocs Explained

Card titled String Templates and Heredocs, showing a for directive that writes a dash and each item, and the result tea, bread and milk on three lines.
Insert values with ${}, repeat lines with %{for}, and write text over several lines

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

SyntaxHCL
"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. <<-EOT also removes the shared indentation.

The full rules are in Strings and Templates (opens in a new tab) in the OpenTofu docs.

Prerequisites

Step 1: Write the Templates

In an empty folder, create main.tf:

main.tfHCL
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."
}
  • greeting puts var.name into a sentence.
  • title adds “(urgent)” only when var.urgent is true.
  • shopping_list is a heredoc whose %{for} repeats one line per item. The ~ stops each directive from leaving an empty line.
  • note is an indented heredoc, <<-EOT, so its lines can follow the indentation of the file.
  • literal writes $${ 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:

TerminalBash
tofu apply
Output (the lines that matter)Plain text
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 fmt removes the spaces inside directives. The docs write %{ for item in var.items ~}; tofu fmt rewrites 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, as shopping_list does.

Clean Up

This example created nothing but a state file. Delete the folder when you’re done.

Common Mistakes

MistakeWhat happensDo this instead
Using a heredoc to write JSON or YAMLA missing comma or quote breaks the file.The docs say to use jsonencode or yamlencode instead.
Writing ${ in text that should keep itOpenTofu 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.

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