HashiCorp Terraform Associate (004) Question 82
Single answer3g Apply formatting and style adjustments to a configurationYour team maintains a Terraform module in Git. A pull request includes several functional changes, but the CI pipeline fails a style check because the contributor manually aligned arguments and used inconsistent spacing across multiple .tf files. The team wants a fast, repeatable way to apply Terraform's standard formatting before review, without changing resource behavior. Which action should the engineer take?
- A
Run
terraform fmt -recursivefrom the module repository root before committing the changes. - B
Run
terraform validate -jsonto automatically rewrite the configuration into the correct style. - C
Run
terraform planso Terraform can normalize formatting when it refreshes the state. - D
Run
terraform apply -auto-approvebecause Terraform rewrites configuration files during apply if style issues are detected.
Show answer and explanation
Correct answer: A
Explanation
For formatting and style adjustments in Terraform configuration, the correct tool is terraform fmt. It rewrites .tf and related Terraform configuration files to a canonical format defined by Terraform, which helps teams maintain consistent style and cleaner code reviews. In practical workflows, teams often run terraform fmt -recursive locally or in CI to enforce formatting across nested module directories. By contrast, terraform validate checks correctness, terraform plan previews infrastructure changes, and terraform apply makes those changes; none of these commands formats configuration files. This aligns with Terraform CLI best practices and the HashiCorp documentation for the fmt command.
- A. Correct.
Correct.
terraform fmtis the Terraform command specifically designed to rewrite configuration files into a canonical format. Using-recursiveis appropriate when the repository contains multiple Terraform files in nested directories, helping enforce consistent style across the module tree. This changes formatting only, not infrastructure behavior. - B. Incorrect.
Incorrect.
terraform validatechecks whether the configuration is syntactically valid and internally consistent, but it does not rewrite files or fix style issues. The-jsonflag only changes the output format of validation results. - C. Incorrect.
Incorrect.
terraform plancompares configuration, state, and real infrastructure to show proposed changes. It does not edit.tffiles or normalize formatting. A common misconception is that Terraform commands that parse configuration also rewrite it, but formatting is handled separately byterraform fmt. - D. Incorrect.
Incorrect.
terraform applyexecutes infrastructure changes; it does not modify source configuration files to fix style. Usingapplyfor a formatting problem would be both unnecessary and risky in a real environment.