Terraform-Associate-004 practice question 16 of 35

Multiple team members are collaborating on infrastructure using Terraform and want to format the* Terraform code following standard Terraform-style convention. How should they ensure the code satisfies conventions?

Full set: 360 questions
Answer options
A: Terraform automatically formats configuration on terraform apply
B: Run terraform validate prior to executing terraform plan or terraform apply
C: Use terraform fmt Correct answer
D: Replace all tabs with spaces
Correct Answer:
C. Use terraform fmt
Explanation

The terraform fmt command is the designated tool within the Terraform CLI for enforcing a standard style. It rewrites Terraform configuration files in the current directory to a canonical format, ensuring consistency in spacing, indentation, and alignment. This command is idempotent and designed to be integrated into team workflows, such as pre-commit hooks or CI/CD pipelines, to automatically maintain a consistent and readable codebase. By using terraform fmt, teams can ensure their code adheres to the standard Terraform style conventions without manual intervention.

Why Incorrect

A. terraform apply executes a plan to create, update, or delete infrastructure; it does not format the configuration files.
B. terraform validate checks configuration files for syntax errors and internal consistency but does not reformat the code.
D. Manually replacing tabs is an incomplete solution; terraform fmt handles all aspects of canonical formatting, including alignment and spacing.

References

1. HashiCorp Terraform Documentation, CLI Commands, fmt: "The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability."

Source: HashiCorp, Terraform CLI Documentation, "fmt Command". Retrieved from https://developer.hashicorp.com/terraform/cli/commands/fmt

2. HashiCorp Terraform Documentation, Language Syntax, Style Conventions: "We recommend running terraform fmt on every Terraform file before committing and pushing it. This will enforce the formatting elements of the style guide."

Source: HashiCorp, Terraform Language Documentation, "Style Conventions". Retrieved from https://developer.hashicorp.com/terraform/language/syntax/style

3. HashiCorp Terraform Documentation, CLI Commands, validate: "The terraform validate command validates the configuration files in a directory... Validation runs a series of checks to verify whether a configuration is syntactically valid and internally consistent..."

Source: HashiCorp, Terraform CLI Documentation, "validate Command". Retrieved from https://developer.hashicorp.com/terraform/cli/commands/validate

4. HashiCorp Terraform Documentation, CLI Commands, apply: "The terraform apply command executes the actions proposed in a Terraform plan to create, update, or destroy infrastructure."

Source: HashiCorp, Terraform CLI Documentation, "apply Command". Retrieved from https://developer.hashicorp.com/terraform/cli/commands/apply

Scroll to Top