How to Format OpenTofu Files with tofu fmt

tofu fmt tidies your OpenTofu files: it fixes the indentation, lines up the = signs and puts everything in OpenTofu’s standard style. This guide shows how to format OpenTofu files with tofu fmt, how to check them without changing anything, and how to see exactly what would change.

It’s a companion to Part 2 of the OpenTofu Tutorial, where tofu fmt checks your first file.

Cost: free. Everything here runs on your own computer.

Every command and output on this page is real, run on OpenTofu 1.13 on macOS. The commands are the same in Windows PowerShell and on Linux.

Prerequisites

Step 1: Start with a Messy File

Here’s the Bookshop block from Part 2, typed carelessly into main.tf. One line has no indent, the other has too much, and the = signs don’t line up:

resource "local_file" "home_page" {
filename = "index.html"
    content = "<h1>Bookshop</h1>"
}

OpenTofu reads it fine, but it’s harder for people to read, and every tidy-up shows up as noise in code reviews.

Step 2: Check the Formatting

-check reports which files need formatting, without changing them:

tofu fmt -check
main.tf

It lists main.tf and ends with an error status, so a check like this can fail a CI/CD pipeline when someone forgets to format.

Step 3: See What Would Change

Add -diff to see the exact changes, still without writing anything:

tofu fmt -check -diff
A Terminal window running tofu fmt -check -diff: it names main.tf and shows a diff in which two badly indented lines are replaced by lines indented two spaces with the equals signs lined up.
tofu fmt -check -diff shows what tofu fmt would change.
main.tf
--- old/main.tf
+++ new/main.tf
@@ -1,4 +1,4 @@
 resource "local_file" "home_page" {
-filename = "index.html"
-    content = "<h1>Bookshop</h1>"
+  filename = "index.html"
+  content  = "<h1>Bookshop</h1>"
 }

Lines starting with - are the current version, and lines starting with + are what tofu fmt would write: both settings indented by two spaces, with their = signs in one column.

Step 4: Format the Files

Run it without options to fix the files:

tofu fmt
main.tf

It prints the name of each file it changed. main.tf now reads:

resource "local_file" "home_page" {
  filename = "index.html"
  content  = "<h1>Bookshop</h1>"
}

Run tofu fmt again and it prints nothing, because there’s nothing left to fix.

Which Files tofu fmt Formats

  • Files ending in .tf, .tofu and .tfvars, plus OpenTofu test files (.tftest.hcl and .tofutest.hcl).
  • Only the current folder. Add -recursive to include every folder below it, which matters once a project has modules.

Common Mistakes

MistakeWhat happensDo this instead
Expecting -check to fix filesNothing changes: -check only reports.Run tofu fmt without options to fix them.
Formatting only the top folderFiles in subfolders stay messy.Use tofu fmt -recursive.
Skipping it after an upgradeOpenTofu’s documentation says the standard style can change slightly between versions.Run tofu fmt again after upgrading OpenTofu.

FAQ

Does tofu fmt change what my configuration does?

No. It only changes the layout: spacing, indentation and alignment. The same plan comes out before and after.

Can my editor run tofu fmt for me?

Yes. The official OpenTofu extension for Visual Studio Code formats files with tofu fmt, and can do it every time you save.

Conclusion

tofu fmt puts your files in OpenTofu’s standard style. Use tofu fmt -check to find untidy files, add -diff to see the fixes, and run plain tofu fmt to apply them. Make it a habit before every commit.

Related

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.

Leave a Reply

Up ↑