HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 82 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 82

Single answer3g Apply formatting and style adjustments to a configuration

Your 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?

  1. A

    Run terraform fmt -recursive from the module repository root before committing the changes.

  2. B

    Run terraform validate -json to automatically rewrite the configuration into the correct style.

  3. C

    Run terraform plan so Terraform can normalize formatting when it refreshes the state.

  4. D

    Run terraform apply -auto-approve because 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 fmt is the Terraform command specifically designed to rewrite configuration files into a canonical format. Using -recursive is 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 validate checks whether the configuration is syntactically valid and internally consistent, but it does not rewrite files or fix style issues. The -json flag only changes the output format of validation results.

  • C. Incorrect.

    Incorrect. terraform plan compares configuration, state, and real infrastructure to show proposed changes. It does not edit .tf files or normalize formatting. A common misconception is that Terraform commands that parse configuration also rewrite it, but formatting is handled separately by terraform fmt.

  • D. Incorrect.

    Incorrect. terraform apply executes infrastructure changes; it does not modify source configuration files to fix style. Using apply for a formatting problem would be both unnecessary and risky in a real environment.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam