HashiCorp Terraform Associate (004) Question 65
Single answer3c Validate a Terraform configurationA platform engineer has updated several Terraform files in a shared module and wants to catch configuration mistakes as early as possible in a CI pipeline, before Terraform contacts any remote APIs or writes a plan. The engineer specifically wants to verify that the configuration is syntactically valid, internally consistent, and refers only to supported argument names and block structures. Which command best fits this requirement?
- A
terraform validate
- B
terraform plan
- C
terraform fmt -check
- D
terraform apply -refresh-only
Show answer and explanation
Correct answer: A
Explanation
The best answer is terraform validate. In Terraform workflows, validation is typically used to confirm that configuration files are syntactically valid and internally consistent before planning or applying. This makes it especially useful in automated pipelines where teams want fast feedback on configuration errors. By contrast, terraform plan is a later-stage command that evaluates proposed infrastructure changes and may interact with providers and remote APIs. terraform fmt -check is valuable for style enforcement but does not validate semantics. According to Terraform CLI best practices and command documentation, terraform validate is intended specifically for configuration validation, usually after terraform init has installed the required providers and modules.
- A. Correct.
Correct.
terraform validatechecks whether a configuration is syntactically valid and internally consistent. It is designed to validate Terraform files without creating a plan or making changes to infrastructure. This is commonly used in CI pipelines after initialization to catch errors such as unsupported arguments, invalid block structure, and incorrect references that can be detected from the configuration itself. - B. Incorrect.
Incorrect.
terraform plandoes more than validation. It creates an execution plan and may require access to providers, state, and remote APIs to evaluate resource changes. Although it can reveal configuration issues, it is not the best answer when the goal is early validation before contacting remote systems or generating a plan. - C. Incorrect.
Incorrect.
terraform fmt -checkverifies formatting only. It helps enforce style consistency but does not validate configuration logic, references, argument names, or block structure. A well-formatted configuration can still be invalid. - D. Incorrect.
Incorrect.
terraform apply -refresh-onlyis used to reconcile state with real infrastructure by refreshing resource data and applying state-only updates if needed. It is not a validation command and is unrelated to checking configuration syntax or structure in a CI validation step.