Skip to content
Latchkey

terraform fmt Command Reference

Canonical formatting for your HCL, enforced automatically.

terraform fmt rewrites configuration files to a canonical style. As a CI check it keeps diffs clean and reviews focused on substance, not whitespace.

What it does

terraform fmt applies the standard Terraform style to .tf and .tfvars files. By default it edits files in place; with -check it only reports whether files are already formatted and exits nonzero if not.

Common flags and usage

  • -check: exit nonzero if files are not formatted (no writes)
  • -recursive: include subdirectories and nested modules
  • -diff: print the formatting diff
  • -write=false: do not modify files

Example

shell
- name: Format check
  run: terraform fmt -check -recursive -diff

In CI

Use terraform fmt -check -recursive as a non-mutating gate. If it fails, run terraform fmt -recursive locally, commit, and push. Drop -check in a pre-commit hook to auto-fix instead of failing the build.

Key takeaways

  • fmt rewrites HCL to a canonical style; -check only reports without writing.
  • Add -recursive to cover nested modules in one pass.
  • Use -check in CI as a gate; run plain fmt locally to fix violations.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →