While building a web page with OpenTofu®, I wrote its book list as an indented heredoc with a %{for} directive and strip markers. The list came out with extra spaces at the start of every line after the directive. Here’s what happened, what showed the cause, and the one-line fix.
What I Was Doing
In Part 5 of the OpenTofu Tutorial, the Bookshop page is built from a list of books with a heredoc template. I wrote it indented, like the rest of the file, with <<-EOT, and put a ~ in each directive so the lines wouldn’t be separated by empty ones:
locals {
heading = "Welcome to the ${var.shop_name}"
home_page = <<-EOT
<h1>${local.heading}</h1>
<p>New this week: ${var.featured_book}.</p>
<ul>
%{for book in var.books~}
<li>${book.title} by ${book.author}</li>
%{endfor~}
</ul>
EOT
}
Then I ran tofu apply.
What I Expected
The OpenTofu docs say an indented heredoc finds the line with the fewest leading spaces and removes that many spaces from every line, and that a strip marker removes the whitespace next to it. So every line should start at the left edge, with the <li> lines two spaces in.
What I Got
The plan showed the page’s new content. The first lines were trimmed, but the <li> lines kept six spaces and </ul> kept four:
~ content = <<-EOT # forces replacement
- <h1>Welcome to the Riverside Bookshop</h1><p>New this week: A Map of Quiet Places by Tomas Vell.</p>
+ <h1>Welcome to the Riverside Bookshop</h1>
+ <p>New this week: A Map of Quiet Places by Tomas Vell.</p>
+ <ul>
+ <li>The Paper Lantern Café by Mira Kale</li>
+ <li>A Map of Quiet Places by Tomas Vell</li>
+ <li>The Clockmaker's Garden by Mira Kale</li>
+ <li>Salt and Starlight by Nadia Quill</li>
+ </ul>
EOT
The page source in the browser showed the same:

What I Checked
I wrote the same list twice in a test file, once with the strip markers and once without, and printed both in tofu console:
locals {
books = ["Book A", "Book B"]
with_strip = <<-EOT
<ul>
%{for book in local.books~}
<li>${book}</li>
%{endfor~}
</ul>
EOT
without_strip = <<-EOT
<ul>
%{for book in local.books}
<li>${book}</li>
%{endfor}
</ul>
EOT
}
> local.with_strip
<<EOT
<ul>
<li>Book A</li>
<li>Book B</li>
</ul>
EOT
> local.without_strip
<<EOT
<ul>
<li>Book A</li>
<li>Book B</li>
</ul>
EOT
> exit
Without the strip markers, every line was trimmed correctly, but empty lines appeared between them. With them, the lines after each directive kept all their spaces. The strip marker is what stops the trimming.
Why It Happens
HCL, the language OpenTofu’s configuration is written in, defines the indented heredoc in its specification: it looks at the text at the start of each line, finds the smallest indentation, and removes that many spaces from all “line-leading” text (HCL Native Syntax Specification: Template Expressions (opens in a new tab)). A strip marker that removes a line break joins the next line onto the directive’s line. That text no longer starts a line, so the indentation isn’t removed from it.
The Fix
Write the template as a flush-left heredoc, <<EOT, as the OpenTofu docs’ own directive examples do. With nothing to remove, every line keeps exactly the spaces you give it:
locals {
heading = "Welcome to the ${var.shop_name}"
home_page = <<EOT
<h1>${local.heading}</h1>
<p>New this week: ${var.featured_book}.</p>
<ul>
%{for book in var.books~}
<li>${book.title} by ${book.author}</li>
%{endfor~}
</ul>
EOT
}
After the Fix
The next tofu apply showed every line coming out as written:
~ content = <<-EOT # forces replacement
<h1>Welcome to the Riverside Bookshop</h1>
<p>New this week: A Map of Quiet Places by Tomas Vell.</p>
<ul>
- <li>The Paper Lantern Café by Mira Kale</li>
- <li>A Map of Quiet Places by Tomas Vell</li>
- <li>The Clockmaker's Garden by Mira Kale</li>
- <li>Salt and Starlight by Nadia Quill</li>
- </ul>
+ <li>The Paper Lantern Café by Mira Kale</li>
+ <li>A Map of Quiet Places by Tomas Vell</li>
+ <li>The Clockmaker's Garden by Mira Kale</li>
+ <li>Salt and Starlight by Nadia Quill</li>
+ </ul>
EOT

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Removing the strip markers to get the trimming back | Every line is trimmed, but empty lines appear between the items. | Keep the strip markers and write the heredoc flush left. |
Indenting the closing word of a <<EOT heredoc | OpenTofu doesn’t find the end of the heredoc. | Only <<-EOT allows spaces before the closing word. |
Troubleshooting
| Problem | Why | Fix |
|---|---|---|
Lines after %{for … ~} or %{endfor~} keep their spaces | They follow a removed line break, so the indented heredoc doesn’t trim them. | Use <<EOT and write the lines flush left. |
| Empty lines between repeated lines | Each directive leaves its own line break. | Add ~ before the closing } of each directive. |
FAQ
Does this break the web page?
No. Browsers ignore the extra spaces in HTML. It matters when the exact text counts, such as a configuration file or a plan you read.
Does tofu fmt fix it?
No. tofu fmt doesn’t change the text of a heredoc; it only removes the spaces inside the directives’ braces.
Conclusion
An indented heredoc (<<-EOT) doesn’t trim the lines that follow a strip marker which removed a line break. Write templates with directives as flush-left heredocs (<<EOT) and every line keeps the spaces you wrote.
Additional Resources
- Strings and Templates (opens in a new tab)
- HCL Native Syntax Specification: Template Expressions (opens in a new tab)
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- OpenTofu String Templates and Heredocs Explained
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