HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 62 of 223

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

HashiCorp Terraform Associate (004) Question 62

Single answer3c Validate a Terraform configuration

A platform engineer updates a Terraform configuration by adding a new variable and refactoring several resource blocks. Before creating an execution plan, they want to verify that the configuration is syntactically valid, internally consistent, and properly formatted across the current working directory. Which sequence of commands best meets this goal?

  1. A

    Run terraform validate and then terraform fmt -check

  2. B

    Run terraform plan and review whether it completes without errors

  3. C

    Run terraform fmt -check and then terraform validate

  4. D

    Run terraform apply -refresh-only to confirm the configuration is valid

Show answer and explanation

Correct answer: C

Explanation

For Terraform configuration validation, the most appropriate commands are terraform fmt and terraform validate, each serving a different purpose. terraform fmt -check confirms whether files match canonical Terraform formatting, which is especially useful in automation because it reports noncompliant files without modifying them. terraform validate checks whether the configuration is syntactically valid and internally consistent, such as validating references, argument structure, and block usage. By contrast, terraform plan is intended to generate an execution plan and may involve provider-specific logic, making it more than is required for simple validation. HashiCorp documentation distinguishes these commands clearly: fmt is for style/formatting, while validate is for correctness of configuration structure after initialization. In practice, teams commonly run terraform fmt -check followed by terraform validate before terraform plan in local workflows or CI pipelines.

  • A. Incorrect.

    terraform validate checks whether the configuration is syntactically valid and internally consistent, but it does not check whether files are formatted according to Terraform style conventions. Running terraform validate first would miss formatting issues until the second step. This option is close, but the question asks for the sequence that best verifies formatting and validity before planning, and the more typical workflow is to format-check first and then validate the resulting configuration.

  • B. Incorrect.

    terraform plan can surface configuration errors, but it is not the best command for basic validation and formatting checks. It also depends on a successfully initialized working directory and may require provider interactions and state inspection. Using plan just to validate syntax and consistency is heavier than necessary and does not verify formatting.

  • C. Correct.

    This is correct. terraform fmt -check verifies whether Terraform configuration files in the working directory conform to standard formatting without rewriting them, which is useful in team workflows and CI pipelines. terraform validate then checks that the configuration is syntactically valid and internally consistent. This sequence is a practical pre-plan validation workflow.

  • D. Incorrect.

    terraform apply -refresh-only is used to reconcile state with real infrastructure without changing configuration-managed resources. It is not intended as a configuration validation command and is much broader and riskier than needed for checking syntax, consistency, and 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