HashiCorp Terraform Associate (004) Question 64
Single answer3c Validate a Terraform configurationA platform engineer is preparing a Terraform configuration for use in a CI pipeline. The pipeline must fail fast if the configuration contains syntax errors, references to undeclared values, or invalid argument usage, but it must not contact the remote backend or cloud provider APIs because credentials are not available in this stage. Which command best meets this requirement when run from the root module directory?
- A
terraform validate
- B
terraform plan
- C
terraform apply -refresh=false
- D
terraform fmt -check
Show answer and explanation
Correct answer: A
Explanation
The best answer is terraform validate. In Terraform workflows, terraform fmt checks style, while terraform validate checks whether a configuration is syntactically valid and internally consistent. This makes it well suited for an early CI stage that should catch configuration errors before planning or applying. By contrast, terraform plan and terraform apply are operational commands that generally require initialized backend/provider access and may interact with state or infrastructure. HashiCorp documentation describes terraform validate as a command to validate the configuration files in a directory, making it a standard best practice for automated checks before running plan or apply.
- A. Correct.
Correct.
terraform validatechecks whether the configuration is syntactically valid and internally consistent. It detects issues such as malformed expressions, references to undeclared resources or variables, and invalid arguments based on the provider schemas already available in the working directory. It is specifically intended as a validation step and does not create a plan or contact remote infrastructure APIs. - B. Incorrect.
Incorrect.
terraform planis used to create an execution plan by comparing configuration against state and, in many cases, refreshing real infrastructure data. This typically requires backend access and provider credentials, so it does not satisfy the requirement to validate the configuration without contacting remote systems. - C. Incorrect.
Incorrect.
terraform apply -refresh=falsestill attempts to perform an apply operation, which is far beyond simple validation. Even with refresh disabled, apply requires planning and execution behavior that depends on backend and provider access. This is not an appropriate command for a fail-fast syntax and configuration validation stage. - D. Incorrect.
Incorrect.
terraform fmt -checkverifies whether files are formatted according to Terraform style conventions, which is useful in CI, but it does not validate configuration semantics. A configuration can be perfectly formatted and still contain invalid references, unsupported arguments, or other errors that onlyterraform validatewould detect.