HashiCorp Terraform Associate (004) Question 85
Single answer3g Apply formatting and style adjustments to a configurationYour team is preparing a Terraform module for code review. Several files were edited by different engineers, and the reviewer asks for the configuration to be normalized to standard Terraform formatting before it is merged. You want to update the files without changing the infrastructure behavior. Which command should you run from the module directory to apply Terraform's standard formatting rules to the configuration files?
- A
terraform validate
- B
terraform fmt
- C
terraform plan
- D
terraform apply
Show answer and explanation
Correct answer: B
Explanation
The correct command is terraform fmt. In real-world workflows, teams commonly run terraform fmt before committing code or as part of CI to enforce consistent style across contributors. This command updates configuration files to Terraform's standard canonical format, helping improve readability and reducing noise in code reviews. By contrast, terraform validate checks correctness, terraform plan previews infrastructure changes, and terraform apply makes changes to real infrastructure. HashiCorp documentation describes terraform fmt as the command used to rewrite Terraform configuration files into a canonical format and recommends it to align code with standard style conventions.
- A. Incorrect.
Incorrect.
terraform validatechecks whether the configuration is syntactically valid and internally consistent, but it does not rewrite files or adjust formatting. A common misconception is that validation also fixes style issues, but it only reports configuration problems. - B. Correct.
Correct.
terraform fmtrewrites Terraform configuration files to a canonical format, including spacing, indentation, and alignment based on Terraform language style conventions. This is the appropriate command when the goal is to apply formatting and style adjustments without changing infrastructure behavior. - C. Incorrect.
Incorrect.
terraform plancompares the current state to the configuration and shows proposed infrastructure changes. It does not modify.tffiles or normalize their formatting. Someone might choose this because it is often used before reviews, but it addresses infrastructure drift and planned changes, not code style. - D. Incorrect.
Incorrect.
terraform applyexecutes planned infrastructure changes against the target environment. It is not related to source formatting and would be risky in this scenario because the team explicitly wants to avoid changing infrastructure behavior.