Module 3: Core Terraform Workflow

Lesson 3.7: Formatting and Style

Plain language

terraform fmt rewrites .tf and .tfvars files into HashiCorp's canonical style. It changes whitespace and layout, never meaning.

Flag Purpose
(none) Format files in the current directory only, in place
-recursive Also format subdirectories, such as modules/
-check Don't write anything. Exit non-zero if any file needs formatting. Use this as a CI gate.
-diff Show the changes it would make
-write=false Don't modify files. Often combined with -diff.

Style conventions fmt enforces

  • Two-space indentation
  • Aligned = signs for consecutive single-line arguments
  • Consistent spacing around blocks

Conventions fmt doesn't enforce

These are HashiCorp style-guide conventions you follow yourself:

  • snake_case names
  • Meta-arguments (count, for_each, provider) first in a block, and lifecycle last
  • A blank line between blocks
  • Standard file names: main.tf, variables.tf, outputs.tf, providers.tf, terraform.tf