Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

3.2.1. terraform fmt

💡 First Principle: Consistent formatting is enforced mechanically rather than argued about, because fmt rewrites configuration to one canonical style — which means diffs stay meaningful (real changes, not whitespace) and reviews stay focused.

terraform fmt rewrites .tf files in the current directory into HashiCorp's canonical style: standard indentation, aligned arguments, and consistent spacing. It changes only layout — never meaning. Useful flags: -recursive (also format subdirectories), -check (exit non-zero if files would change, without rewriting — ideal for CI), and -diff (show what would change).

⚠️ Exam Trap: fmt never alters logic, renames anything, or fixes errors — it only adjusts whitespace and styling. An answer claiming fmt "corrects configuration mistakes" is wrong. Also note -check makes it a non-destructive gate, which is how CI uses it.

Reflection Question: Why is terraform fmt -check preferred over plain terraform fmt inside a CI pipeline?

Alvin Varughese
Written byAlvin Varughese
Founder18 professional certifications