HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 81 of 223

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

HashiCorp Terraform Associate (004) Question 81

Single answer3g Apply formatting and style adjustments to a configuration

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

  1. A

    Run terraform fmt -recursive in the repository and have contributors use it before committing changes.

  2. B

    Run terraform validate in the repository because it rewrites configuration files into Terraform's canonical style.

  3. C

    Run terraform plan and review the output because it automatically reformats configuration files when differences are detected.

  4. D

    Run terraform init -upgrade so 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 fmt is the Terraform command specifically designed to rewrite configuration files into a canonical format, including spacing, indentation, and other style adjustments. Using -recursive is 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 validate checks 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 plan compares the current state with the desired configuration and shows proposed infrastructure changes. It does not modify .tf files 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 -upgrade initializes 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.

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