HashiCorp Terraform Associate (004) Question 83
Single answer3g Apply formatting and style adjustments to a configurationYour team stores Terraform configurations in Git and enforces a pre-commit check before merging. A teammate submits a change that adds new resources, but the pre-commit pipeline fails because the configuration does not match the team's required Terraform formatting style. The resources and arguments are valid, and you do not want to change the behavior of the infrastructure. What is the most appropriate action to make the configuration comply with Terraform's standard formatting expectations before committing the change?
- A
Run
terraform fmtin the working directory and commit the resulting formatting-only changes - B
Run
terraform validateto automatically rewrite the configuration into canonical Terraform style - C
Run
terraform planso Terraform can detect formatting issues and update the files before apply - D
Manually reorder all blocks and arguments to match the order shown in the provider documentation
Show answer and explanation
Correct answer: A
Explanation
The best answer is to run terraform fmt. In Terraform workflows, formatting is handled separately from validation and planning. terraform fmt automatically updates configuration files to Terraform's canonical style, making code easier to read and consistent across contributors without changing infrastructure behavior. By contrast, terraform validate checks correctness, and terraform plan previews infrastructure changes. HashiCorp documentation identifies terraform fmt as the command used to rewrite Terraform configuration files to a standard format, and it is commonly integrated into CI/CD and pre-commit processes for team-based development.
- A. Correct.
Correct.
terraform fmtrewrites Terraform configuration files into a canonical format without changing their behavior. It is the standard command used to apply formatting and style adjustments to.tfand related configuration files. This is commonly used in local workflows, CI pipelines, and pre-commit hooks to ensure consistent style across a team. - B. Incorrect.
Incorrect.
terraform validatechecks whether the configuration is syntactically valid and internally consistent, but it does not rewrite files or apply formatting changes. A common misconception is that validation also fixes style issues, but formatting and validation are separate Terraform commands. - C. Incorrect.
Incorrect.
terraform plancompares the current state and configuration to propose infrastructure changes. It does not edit configuration files or enforce style formatting. Someone might choose this option because plan is often used before apply, but it is unrelated to rewriting source files for style compliance. - D. Incorrect.
Incorrect. Manual reordering is unnecessary and error-prone. Terraform's standard formatting should be applied with
terraform fmt, not by trying to imitate examples from documentation. In addition, provider documentation examples are not a canonical formatting enforcement mechanism, and argument order usually does not affect behavior.