HashiCorp Terraform Associate (004) Question 81
Single answer3g Apply formatting and style adjustments to a configurationYour team receives frequent pull request comments about inconsistent indentation, argument alignment, and spacing across Terraform files in a shared module repository. You want a simple, repeatable way to apply Terraform's standard formatting rules before code is reviewed, without changing infrastructure behavior. Which action is the best choice?
- A
Run
terraform fmt -recursivein the repository and have contributors use it before committing changes. - B
Run
terraform validatein the repository because it rewrites configuration files into Terraform's canonical style. - C
Run
terraform planand review the output because it automatically reformats configuration files when differences are detected. - D
Run
terraform init -upgradeso Terraform downloads the latest providers and updates the configuration style automatically.
Show answer and explanation
Correct answer: A
Explanation
The best answer is to use terraform fmt, which is the built-in Terraform command for applying canonical formatting to configuration files. In real workflows, teams often run terraform fmt locally or in CI to reduce noisy code review comments and keep .tf files consistent. The -recursive flag is especially useful for repositories with nested modules or environment directories. By contrast, terraform validate checks correctness, terraform plan previews infrastructure changes, and terraform init prepares the working directory and installs dependencies. HashiCorp documentation identifies terraform fmt as the standard tool for formatting configuration according to Terraform style conventions.
- A. Correct.
Correct.
terraform fmtis the Terraform command specifically designed to rewrite configuration files into a canonical format, including spacing, indentation, and other style adjustments. Using-recursiveis appropriate when the repository contains Terraform files in subdirectories, such as multiple modules or environment folders. This improves consistency without altering resource behavior. - B. Incorrect.
Incorrect.
terraform validatechecks whether the configuration is syntactically valid and internally consistent, but it does not rewrite files or enforce formatting. A common misconception is that validation includes style correction, but formatting and validation are separate concerns in Terraform. - C. Incorrect.
Incorrect.
terraform plancompares the current state with the desired configuration and shows proposed infrastructure changes. It does not modify.tffiles or apply formatting rules. Someone might choose this option because plan is often used before review, but it is for change preview, not code style normalization. - D. Incorrect.
Incorrect.
terraform init -upgradeinitializes the working directory and can upgrade provider and module dependencies, but it does not reformat Terraform configuration files. This distractor reflects confusion between dependency management and source code formatting.