Domain 3 of 8 · Chapter 7 of 7

Apply formatting and style adjustments

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing HashiCorp Certified: Terraform Associate premium course — no separate purchase.

14-day money-back guarantee — no questions asked.

Included in this chapter:

  • What canonical formatting means
  • Running fmt: write, check, diff, and scope
  • Where fmt fits: pre-commit and CI
  • Exam-pattern recognition

terraform fmt invocations at a glance

InvocationWrites files?Non-zero exit if unformatted?Typical use
terraform fmtYes, in placeNoFormat the current directory before a commit
terraform fmt -recursiveYes, including subdirectoriesNoFormat a tree of nested modules
terraform fmt -checkNoYesCI gate: fail when anything is unformatted
terraform fmt -diffYes, unless -check or -write=falseNoShow the changes fmt would make
terraform fmt -No, writes to stdoutNoFormat piped or generated HCL via stdin

Decision tree

Format files, orpipe a snippet?terraform fmt -stdin to stdoutRewrite files, orcheck only?Includesubdirectories?CI gate,or preview?terraform fmtin placeterraform fmt -recursivesubdirectoriesterraform fmt -checknon-zero exitterraform fmt -check -diffpreview, no writePipe (stdin)Format filesRewriteCheckCurrent dirSubdirsCI gatePreview

Cheat sheet

  • fmt rewrites files to canonical style
  • fmt formats, it does not validate
  • -check verifies formatting without writing
  • -diff shows formatting changes
  • -recursive extends fmt into subdirectories
  • fmt can read from stdin

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. Command: terraform fmt
  2. Terraform language style conventions
  3. Terraform style guide
  4. Command: terraform validate
  5. Command: terraform plan