Lesson 3 of 4 Initializing, Validating and Formatting Configuration

Validating Configuration

terraform validate checks configuration without contacting any remote system. This lesson explains what it catches, what it misses and how it is used.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what terraform validate checks and what it cannot check.
  • Describe how validate is used in development and CI.

What validate checks

terraform validate parses every .tf file in the working directory and checks that the configuration is syntactically valid and internally consistent. It catches errors such as a missing closing brace, an argument name that the provider's schema does not define, a missing required argument, a value of the wrong type, and a reference to a variable, resource or module that does not exist.

$ terraform validate
Error: Reference to undeclared input variable

  on main.tf line 4, in resource "local_file" "motd":
   4:   content = var.mesage

An input variable with the name "mesage" has not been declared. Did you mean "message"?

Because validate checks argument names and types against provider schemas, the providers must be installed, so run terraform init first. In environments without backend access, terraform init -backend=false installs providers and modules without configuring the backend.

Sort into groups

Caught by validate?

Exam skills 3.3.1

Decide whether terraform validate catches each problem or it appears only in plan or apply.

Log in to try this activity Interactive activities are free with an account.

What validate does not check

validate never reads state and never calls provider APIs. It therefore cannot know whether an image ID exists, whether a bucket name is already taken, whether credentials work or whether the configuration would change anything. Those problems surface in plan or apply.

terraform validate
Syntax and argument namesYes
References and typesYes
Reads state and real infrastructureNo
Needs credentialsNo
Shows proposed changesNo
terraform plan
Syntax and argument namesYes
References and typesYes
Reads state and real infrastructureYes
Needs credentialsYes, for providers and backend
Shows proposed changesYes

The following diagram shows where a value that is valid syntax but wrong for the provider is caught.

Where an image ID that does not exist is caught. terraform validate is offline: the argument name and string type are correct, so it reports success. Only a provider API call can discover that the image is missing, which happens during apply; a data source that looks up the image, or a precondition, would surface it during plan.
The image ID is illustrative.

Using validate in development and CI

Editors and pre-commit hooks run validate to give feedback in seconds. In continuous integration, a check stage typically runs these commands before any plan, so broken code fails quickly and without credentials.

terraform fmt -check -recursive
terraform init -backend=false -input=false
terraform validate

terraform validate -json produces machine-readable diagnostics for tools. Plan and apply validate the configuration themselves, so validate is about speed and early feedback, not an additional safety check.

Put in order

A CI check stage

Exam skills 3.3.2 · 3.7.1

Arrange the commands of a fast CI check stage that needs no cloud credentials.

Log in to try this activity Interactive activities are free with an account.
Worked example

Valid but wrong

A configuration sets ami = "ami-12345678" on an aws_instance, but that image does not exist in the region. terraform validate reports "Success! The configuration is valid." Why?

The decisive fact is that validate is offline.

The value is a string, which is the correct type, and the argument name is correct, so the configuration is internally consistent. Whether the image exists can only be discovered by calling the AWS API, which validate never does. The error appears during terraform apply, when the provider tries to launch the instance. A data source that looks up the image, or a precondition, would catch it during plan.

Check yourself

Can terraform validate detect that a referenced image ID does not exist?

Show answer

No. validate is offline: it checks syntax and internal consistency but never calls provider APIs.

Exam Tips

Remember for the exam

  • validate checks syntax and consistency only, offline.
  • Run init (optionally -backend=false) before validate.
  • validate cannot detect missing images, taken names or bad credentials.