⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 44 of 120 in Terraform & IaC
Senior DevOps / SRE [L2] Terraform Terraform Modules & State Production Scenario [L2]
🎯 Target Role / Context: Terraform Scenario #39 (L2) · Use VPC ID from another module

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.

#terraform fmt recursive #terraform fmt -recursive #terraform fmt -check #terraform fmt #IaC Linting #Terraform CI CD #pre-commit hook #Terraform #IaC #DevOps
🎙️ Candidate Opening & Architectural Context
""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.""
Advertisement
⚡ Recommended Practice Lab

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."

1️⃣

Standard terraform fmt vs terraform fmt -recursive

Understanding directory traversal limitations:

  • Standard terraform fmt: Only parses and formats .tf and .tfvars files 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.
2️⃣

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 code 0 if all files are properly formatted; returns exit code 3 if 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.
Advertisement
3️⃣

Shifting Left with Git Pre-Commit Hooks

Prevent unformatted code from ever entering Git repositories:

  • Add the Anton Babenko terraform-docs / pre-commit-terraform hook in .pre-commit-config.yaml:
  • - id: terraform_fmt with args: ['--args=-recursive'].
  • Developers receive instant feedback at git commit time, automatically reformatting staged files locally before push.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Always run terraform fmt -recursive -check -diff in CI pipelines to prevent unformatted IaC drift, and equip engineers with git pre-commit hooks for zero-friction local formatting."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →