HashiCorp Terraform Associate (004) Question 62
Single answer3c Validate a Terraform configurationA 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?
- A
Run
terraform validateand thenterraform fmt -check - B
Run
terraform planand review whether it completes without errors - C
Run
terraform fmt -checkand thenterraform validate - D
Run
terraform apply -refresh-onlyto 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 validatechecks whether the configuration is syntactically valid and internally consistent, but it does not check whether files are formatted according to Terraform style conventions. Runningterraform validatefirst 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 plancan 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. Usingplanjust to validate syntax and consistency is heavier than necessary and does not verify formatting. - C. Correct.
This is correct.
terraform fmt -checkverifies 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 validatethen checks that the configuration is syntactically valid and internally consistent. This sequence is a practical pre-plan validation workflow. - D. Incorrect.
terraform apply -refresh-onlyis 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.