HashiCorp Terraform Associate (004) Question 84
Single answer3g Apply formatting and style adjustments to a configurationA 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?
- A
Run
terraform fmt -recursivein the module repository before committing changes - B
Run
terraform validateto automatically rewrite the files into Terraform's canonical style - C
Run
terraform planso Terraform can normalize the configuration during the planning phase - D
Run
terraform apply -refresh-onlyto 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 fmtrewrites Terraform configuration files to a canonical format, including spacing and indentation. Using the-recursiveflag 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 validatechecks 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 plancompares configuration against state and remote objects to show proposed infrastructure changes. It does not rewrite.tffiles 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-onlyrefreshes 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.