HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 84 of 223

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

HashiCorp Terraform Associate (004) Question 84

Single answer3g Apply formatting and style adjustments to a configuration

A platform team is preparing a Terraform module for code review. The configuration is functionally correct, but reviewers keep rejecting commits because spacing, indentation, and argument alignment are inconsistent across several .tf files. The team wants the fastest, repeatable way to apply Terraform's standard formatting rules before every commit without changing resource behavior. Which action should they take?

  1. A

    Run terraform fmt -recursive in the module repository before committing changes

  2. B

    Run terraform validate to automatically rewrite the files into Terraform's canonical style

  3. C

    Run terraform plan so Terraform can normalize the configuration during the planning phase

  4. D

    Run terraform apply -refresh-only to update state and rewrite the configuration formatting

Show answer and explanation

Correct answer: A

Explanation

The correct tool for applying formatting and style adjustments to Terraform configuration is terraform fmt. It rewrites configuration files according to Terraform's canonical style, making code reviews easier and reducing formatting-related noise in commits. In a real repository with nested modules, terraform fmt -recursive is commonly used so subdirectories are formatted in one step. This command changes only file formatting, not the meaning of the configuration or the managed infrastructure. By contrast, terraform validate checks correctness, terraform plan previews infrastructure changes, and terraform apply -refresh-only updates state information. HashiCorp documentation identifies terraform fmt as the command specifically intended to format Terraform configuration files consistently according to language style conventions.

  • A. Correct.

    Correct. terraform fmt rewrites Terraform configuration files to a canonical format, including spacing and indentation. Using the -recursive flag is appropriate when the repository contains multiple directories or modules, because Terraform will format matching files in subdirectories as well. This is the standard, repeatable approach for style and formatting adjustments without changing infrastructure behavior.

  • B. Incorrect.

    Incorrect. terraform validate checks whether a configuration is syntactically valid and internally consistent, but it does not modify files or apply style changes. A common misconception is that validation also fixes formatting issues, but formatting and validation are separate commands.

  • C. Incorrect.

    Incorrect. terraform plan compares configuration against state and remote objects to show proposed infrastructure changes. It does not rewrite .tf files or enforce style conventions. Someone might choose this because plan is often used before review, but it is unrelated to source formatting.

  • D. Incorrect.

    Incorrect. terraform apply -refresh-only refreshes state to reflect real infrastructure without proposing configuration-driven changes. It affects state and planning behavior, not the layout or style of Terraform configuration files. This option confuses operational state management with 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