> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Terraform Validate

> Explains terraform validate, an offline local syntax and schema check for Terraform configurations that requires terraform init and does not call provider APIs.

In this lesson we cover `terraform validate` — the Terraform CLI command that performs a local, offline check of your configuration files to catch syntax and structural errors before you run slower, provider-interacting commands.

What `terraform validate` does

* Checks HCL syntax across your configuration files.
* Validates structural correctness of blocks (resources, modules, providers).
* Verifies required arguments and attribute types against provider schemas (when initialized).
* Confirms referenced variables and outputs are declared.

What `terraform validate` does NOT do

* Make API calls to cloud providers or external services.
* Confirm provider-side resources exist (for example, whether an AMI ID is valid).
* Check cloud permissions or runtime environment constraints.
* Guarantee an apply will succeed.

<Callout icon="lightbulb" color="#1CB2FE">
  `terraform validate` performs an offline schema-aware validation of your configs. It relies on provider schemas downloaded during `terraform init`, but it does not make any provider API calls, so provider-side problems (missing AMIs, permissions, quotas) will not be detected here.
</Callout>

Example resource snippet (what `terraform validate` inspects for syntax and attribute correctness):

```hcl theme={null}
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"

  tags = {
    Name = "example"
  }
}
```

Because validation is offline, it will not detect provider-level issues such as:

* Whether the referenced AMI actually exists.
* Whether you have permissions to create the resource.
* Whether the resource already exists in the provider.

Those problems are detected later by `terraform plan` and `terraform apply`, which interact with provider APIs.

<Callout icon="warning" color="#FF6B6B">
  You must run `terraform init` before `terraform validate`. Initialization downloads provider plugins and schemas that `validate` uses. Running `validate` without first initializing the working directory will fail and prompt you to run `terraform init`.
</Callout>

Where `terraform validate` fits in a typical Terraform workflow

| Step       | Command              | Purpose                                                                       |
| ---------- | -------------------- | ----------------------------------------------------------------------------- |
| Initialize | `terraform init`     | Download providers, modules, and schema required for validation and planning. |
| Validate   | `terraform validate` | Fast, local syntax and schema check.                                          |
| Plan       | `terraform plan`     | Evaluate changes against provider state; makes API calls.                     |
| Apply      | `terraform apply`    | Apply changes to your infrastructure.                                         |
| Destroy    | `terraform destroy`  | Remove infrastructure created by Terraform.                                   |

Example command sequence

```bash theme={null}
terraform init
terraform validate
terraform plan
terraform apply
terraform destroy
```

Best practices and tips

* Use `terraform validate` as a quick pre-commit or CI check to catch syntax/regression issues before running expensive plans.
* Combine `terraform validate` with `terraform fmt -check` in CI to ensure consistent formatting and valid syntax.
* Remember that `validate` is not a substitute for `terraform plan` — always review the plan before applying.

Exam tip

* For the Terraform Associate exam and practical use, remember: `terraform validate` is an offline syntax/structure check and requires `terraform init` to have been run in the working directory.

Links and references

* [Terraform CLI Docs — validate](https://www.terraform.io/cli/commands/validate)
* [Terraform Init](https://www.terraform.io/cli/commands/init)
* [Terraform Plan](https://www.terraform.io/cli/commands/plan)
* [HashiCorp Certified: Terraform Associate](https://learn.hashicorp.com/collections/terraform/terraform-associate)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/5b03b9b7-5f0f-4df6-8506-7de492c4791d/lesson/84faaf55-4299-4f18-a2ff-77a55feb1b90" />
</CardGroup>
