terraform validate and terraform fmt to catch configuration errors early and enforce consistent HCL (HashiCorp Configuration Language) formatting.
Overview
terraform validateperforms static validation of Terraform configuration files. When run afterterraform init, it loads provider schemas and checks configuration structure and types against those schemas.terraform fmtreformats HCL files to the canonical style defined by HashiCorp. It changes only formatting (whitespace, indentation) and does not alter any configuration logic.
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
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:
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:
terraform fmt -check to verify that files are already formatted and fail the build if they are not:
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 initonce per working directory (or in your CI job) beforeterraform validate. - Add
terraform fmt -checkandterraform validateto CI pipelines to block malformed or invalid configs. - Use
terraform fmtas a pre-commit hook to keep repository diffs clean and consistent. - Remember that
terraform validateis static — to confirm resource creation and runtime constraints, useterraform planandterraform applyin a safe environment.
Links and references
Summary:- Use
terraform validateto catch syntax and provider-schema issues before planning/applying. - Use
terraform fmtto 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 planorterraform apply.