Skip to main content
This lesson explains how to use terraform validate and terraform fmt to catch configuration errors early and enforce consistent HCL (HashiCorp Configuration Language) formatting.

Overview

  • terraform validate performs static validation of Terraform configuration files. When run after terraform init, it loads provider schemas and checks configuration structure and types against those schemas.
  • terraform fmt reformats HCL files to the canonical style defined by HashiCorp. It changes only formatting (whitespace, indentation) and does not alter any configuration logic.
Both commands are fast and lightweight and are ideal for local development checks and CI pipelines.

terraform validate — static validation (syntax + provider schema)

terraform validate parses your configuration, checks HCL syntax, and — if you previously ran terraform init — uses provider schemas to validate resource and argument names and types. Important: run terraform init first so Terraform can download provider plugins and schema information. Without initialization, terraform validate still checks basic HCL syntax but may not catch provider-specific schema errors. Example: an Azure Storage Account resource with a subtle argument-name typo
At a glance the block looks valid, but the correct argument name is account_replication_type (singular). The typo results in a missing required argument plus an unsupported unexpected argument. After running terraform init and terraform validate, Terraform will detect both issues and report file/line numbers with suggestions:
Key characteristics of terraform validate:
  • Validates HCL syntax and configuration structure.
  • When initialized, validates against provider schemas (argument names, types, required fields).
  • Does not make API calls to create or modify cloud resources — no infrastructure changes.
  • Excellent to use in pre-commit hooks and CI pipelines to catch schema and syntax problems early.
Run terraform init before terraform validate to ensure provider schemas are available for full validation.

terraform fmt — enforce consistent HCL formatting

terraform fmt reformats your Terraform configuration files to the canonical HCL style. This improves readability and reduces noisy diffs in version control. It does not check correctness or change resource semantics. Example of a poorly formatted file:
Format the file with:
For CI pipelines, use terraform fmt -check to verify that files are already formatted and fail the build if they are not:
Notes about terraform fmt:
  • Only modifies whitespace/indentation and formatting.
  • Does not validate argument names, types, or provider schemas.
  • Useful as an automatic pre-commit hook step or CI gate to enforce a consistent code style.
terraform validate does not check runtime constraints or whether a given resource name is already taken by the cloud provider. Those checks require interacting with the provider (for example via terraform plan/apply or provider APIs).

Quick reference

Best practices

  • Always run terraform init once per working directory (or in your CI job) before terraform validate.
  • Add terraform fmt -check and terraform validate to CI pipelines to block malformed or invalid configs.
  • Use terraform fmt as a pre-commit hook to keep repository diffs clean and consistent.
  • Remember that terraform validate is static — to confirm resource creation and runtime constraints, use terraform plan and terraform apply in a safe environment.
Summary:
  • Use terraform validate to catch syntax and provider-schema issues before planning/applying.
  • Use terraform fmt to enforce consistent HCL formatting across your team.
  • Both commands are quick and should be part of your standard Terraform workflow (locally and in CI) before running terraform plan or terraform apply.
We’ll now cover another Terraform command.

Watch Video