004 · Domain 3: Core Terraform workflow · 13% of the exam
Task 3.3: Validate a Terraform configuration
terraform validate: syntax and internal consistency checked without contacting remote services, what it needs from init, and what it cannot catch.
Study it
terraform validate and what it cannot catch
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
terraform validate passes on a configuration, but the following apply fails because the requested machine type is not offered in the chosen region. Why didn't validate catch it?
Avalidate checks only the root module and skips the resources in child modules
Bvalidate was run without -json, which is needed for the full provider checks
Cvalidate uses cached API results from the last plan, which predated the region change
Dvalidate checks syntax and internal consistency only and never calls provider APIs
Show the answer and why
Avalidate checks only the root module and skips the resources in child modules
Incorrect
validate covers the whole configuration, including installed child modules.
Bvalidate was run without -json, which is needed for the full provider checks
Incorrect
-json changes the output format, not what is checked.
Cvalidate uses cached API results from the last plan, which predated the region change
Incorrect
validate does not read plans or call APIs at all.
Dvalidate checks syntax and internal consistency only and never calls provider APIs
Correct
validate verifies that the configuration is syntactically valid and internally consistent; it does not validate remote services such as provider APIs.
validate answers "is this a well-formed configuration?", not "will the platform accept it?"; only plan and apply talk to the APIs.
A new CI job checks out a repository and runs terraform validate as its first command. The configuration calls a module from the Terraform Registry and requires two providers. The same command passes on the developer's laptop but fails in the job. What does the job need to do?
ARun terraform plan first, so validate has a plan to compare against
BRun terraform init first, so the module and both providers are installed
CRun terraform get first, which downloads the module and both providers
DCopy in the laptop's state file first, as validate compares against state
Show the answer and why
ARun terraform plan first, so validate has a plan to compare against
Incorrect
validate does not read plans; it checks the configuration files themselves, and a plan would also need an initialized working directory.
BRun terraform init first, so the module and both providers are installed
Correct
Validation requires an initialized working directory with any referenced plugins and modules installed, and terraform init installs both.
CRun terraform get first, which downloads the module and both providers
Incorrect
terraform get downloads modules only and skips the other initialization tasks, so the providers would still be missing.
DCopy in the laptop's state file first, as validate compares against state
Incorrect
validate checks the configuration regardless of any existing state, so a state file does not help.
A laptop that has run terraform init already has the providers and modules installed; a fresh checkout does not, so the job runs init before validate.