Q: What does `terraform fmt -recursive` do, how does it differ from standard `terraform fmt`, and how do you integrate automated formatting checks into CI/CD pipelines and pre-commit hooks?
Guide to using terraform fmt -recursive: formatting nested module directory trees, running non-modifying CI validation checks with -check and -diff, and enforcing Git pre-commit hooks.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "In our enterprise Terraform repository, we designed reusable modules and remote backends to prevent this exact issue. When addressing this question, I walk the interviewer through our production incident runbook: isolating the blast radius, checking diagnostic logs and metrics, and applying a safe fix."
Standard terraform fmt vs terraform fmt -recursive
Understanding directory traversal limitations:
- Standard
terraform fmt: Only parses and formats.tfand.tfvarsfiles located directly in the current working directory. It completely ignores nested child modules or subfolders. - Recursive
terraform fmt -recursive: Recursively scans every subfolder and nested module in the entire directory tree, applying canonical HashiCorp HCL style (2-space indent, equal-sign alignment, trailing commas). - In-Place Modification: Rewrites files in place and outputs the list of modified file paths to stdout.
Automating Formatting Checks in CI/CD (-check & -diff)
In automated pull request workflows, CI runners must verify formatting without silently mutating files:
- The CI Command:
terraform fmt -recursive -check -diff -check: Returns exit code0if all files are properly formatted; returns exit code3if any files require formatting (failing the pipeline PR check).-diff: Prints unified diffs directly into the CI build log showing the exact lines with spacing or indentation violations.
Shifting Left with Git Pre-Commit Hooks
Prevent unformatted code from ever entering Git repositories:
- Add the Anton Babenko
terraform-docs / pre-commit-terraformhook in.pre-commit-config.yaml: - id: terraform_fmtwithargs: ['--args=-recursive'].- Developers receive instant feedback at
git committime, automatically reformatting staged files locally before push.
- Standardized enterprise IaC repos with automated terraform fmt -recursive -check -diff in GitHub Actions PR gates.
- Configured git pre-commit hooks to automatically format nested modules before commit, eliminating CI formatting failures.
- Maintained canonical HashiCorp HCL alignment and readability across hundreds of shared Terraform modules.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Executing Terraform applies without inspecting '# forces replacement' warnings in plan output when configuring Terraform Recursive Format Attributes that force resource replacement destroy and recreate critical infrastructure (databases, load balancers), inducing unannounced production outages.
- Omitting remote state locking mechanisms (DynamoDB or backend native locks) for shared state files when configuring Terraform Recursive Format Without state locking, concurrent CI/CD pipeline executions or team applies create race conditions that corrupt the state file.
- Structuring entire enterprise cloud footprints into a single monolithic Terraform root module when configuring Terraform Recursive Format Monolithic state files slow plan execution dramatically, increase blast radius during failed applies, and create severe pipeline locking contention.
- Failing to pin required_providers versions, causing breaking provider upgrades in CI pipelines when configuring Terraform Recursive Format Unpinned provider versions automatically download breaking major upgrades during CI runs, introducing unexpected schema validation failures.