TutorialsTerraformCommands › terraform fmt

Terraform Commands · Command 2 of 8

terraform fmt

Rewrites .tf files into Terraform's canonical formatting — consistent indentation, aligned = signs, consistent spacing. It never changes what a configuration means, only how it looks on the page.

Codey the Sr Developer, standing with a pointer, ready to walk through the guide
Changes meaning → never Needs init first → no Used in CI as → a check, not a rewrite

What this command does in detail

fmt is purely cosmetic. It parses each .tf file, and rewrites it using Terraform's standard style rules: two-space indentation, argument values aligned into neat columns inside a block, consistent blank lines. Two people writing the same configuration by hand might format it slightly differently — fmt removes that variation, so every file in the project reads the same way no matter who last edited it.

Unlike every other command on this page, fmt never contacts Azure, never reads state, and doesn't even need terraform init to have been run first — it only needs valid HCL syntax to parse.

Where to run it from working directory

terraform -chdir=terraform fmt -recursive

-recursive also formats files in subdirectories — in this project, that includes terraform/bootstrap/. Without it, fmt only touches .tf files directly inside the folder you ran it from.

Precondition what must already be true

  • Terraform CLI is installed. Nothing else — no init, no Azure login, no state.
  • Every file being formatted has to be syntactically valid HCL. fmt can fix spacing around broken syntax in some cases, but it cannot invent missing braces or fix a genuinely malformed file.

Postcondition what becomes true after it succeeds

  • Every .tf file in scope is rewritten in place to canonical formatting (unless -check was used — see below).
  • No resources, state, or configuration meaning changed at all — only whitespace and alignment.
  • The command prints the names of any files it changed, or nothing if everything was already formatted.

How this project uses it real examples

In CI: check, don't rewrite.github/workflows/terraform.yml
terraform -chdir=terraform fmt -check -recursive

-check changes the command's behavior: instead of rewriting files, it only reports whether any file would be changed, and exits with a non-zero status if so — which fails the CI job. This is deliberate: an automated pipeline shouldn't silently rewrite and commit code on your behalf; it should tell a human the formatting is off so they can fix it locally.

Locally: actually fix itbefore committing
terraform -chdir=terraform fmt -recursive

Run this (without -check) any time before committing, so the CI check above passes on the first try.

Common errors and how to fix them

What you seeLikely causeHow to fix it
CI fails on fmt -check -recursiveSomeone committed a .tf file that wasn't run through fmt first.Run terraform fmt -recursive locally, review the diff, and commit the result.
“Argument or block definition required” / parse errorThe file has an actual HCL syntax error — a missing brace, quote, or equals sign — not just messy formatting.fmt can't fix broken syntax; open the file and correct the structure by hand, then re-run fmt.
Nothing happens / no files listedEverything was already correctly formatted.Nothing to fix — this is the expected, successful result.
Codey giving a thumbs up

Tidy files, same meaning — next, a command that actually checks that meaning is correct.