Skip to main content
Terraform validation helps you catch configuration and business-rule problems before they reach production. In this guide you’ll learn the four validation mechanisms Terraform provides, when each runs during the lifecycle, and practical examples to enforce safe infrastructure changes. Imagine deploying on a Friday afternoon. A teammate runs terraform apply and the plan looks fine, but an EC2 instance fails to boot because an ARM AMI was paired with an x86_64 instance type. The HCL was syntactically valid, yet incompatible. Validation catches these semantic problems earlier so you avoid cascading failures and costly troubleshooting. Terraform offers four validation layers:
  • Variable validation
  • Preconditions
  • Postconditions
  • Checks
Think of these as defensive layers that run at different points during planning and apply. Understanding each stage helps you apply the right guardrail for your team and automation workflow (also useful when preparing for the Terraform Associate certification). So here’s where each validation fits into the workflow:
A slide titled "Validating Your Configuration" showing a left-to-right workflow (Variable Validation → Preconditions → Postconditions → Checks) with purple icons and a long "Terraform Workflow" arrow underneath. The slide uses simple pictograms for checklist, pie chart, flow diagram, and a check-marked inbox.
  • Variable validation: runs immediately when Terraform creates a plan (before contacting providers). If it fails, Terraform stops.
  • Preconditions: evaluated when Terraform generates the plan but before it creates any resources.
  • Postconditions: evaluated after a resource has been created or updated (to verify the result).
  • Checks: run as the final step of a plan or apply. They produce warnings rather than errors and do not block the operation.
Variable validation Variable validation validates inputs at plan time and prevents bad inputs from progressing to provider APIs. Use it to enforce naming conventions, region allowlists, numeric ranges, and other input-level business rules. Template for variable validation:
Notes:
  • condition must evaluate to true or false.
  • error_message displays when condition is false.
  • Validation runs before contacting providers, giving fast and user-friendly feedback.
Examples
  1. Allowed Azure regions (allowlist)
This prevents accidental deployments to disallowed regions such as westeurope or southeastasia.
  1. Numeric validation with business rules
  1. Multiple validation rules on one variable
You can combine rules inside a single validation block or use multiple validation blocks (if your Terraform version supports stacking). Each rule is evaluated independently.
Preconditions and postconditions Preconditions and postconditions attach to resources inside a resource’s lifecycle block. Use preconditions to verify assumptions before creating/updating a resource, and postconditions to assert that the resource ended up in the expected state after creation or update. Both use the same structure: a condition and an error_message. Precondition template:
Postcondition example:
Key points:
  • precondition runs after planning but before any create/update actions. If it fails, Terraform aborts the apply.
  • postcondition runs after the resource has been created or updated. Use self to reference the resource’s attributes. If it fails, Terraform aborts and shows the error.
  • Use these to guard incompatible actions (precondition) or to assert that provisioning achieved the expected runtime state (postcondition).
Checks (check blocks) Check blocks are top-level blocks executed as the final step of a plan or apply. They can reference resources, data sources, variables, and outputs. Unlike variable validation, preconditions, and postconditions, check block assertions produce warnings rather than errors — they do not block the operation.
A presentation slide about Terraform "Checks (via check Blocks)" listing bullets on validating resources, infrastructure behavior, non-blocking verification, and that the check block runs as the last step after plan or apply. The slide has a purple gradient background and a Terraform logo illustration.
Example check block monitoring a website health endpoint:
Notes:
  • If the assertion fails, Terraform emits a warning with the error_message, but the deployment continues.
  • Check blocks are ideal for non-blocking verification such as endpoint health, certificate expiry, or runtime checks.
  • On HCP Terraform, check blocks can run continuously to monitor infrastructure health over time.
Choosing the right validation Use the mechanism that matches when and how you want to block or warn:
A purple presentation slide titled "Choosing the Right Validation" that maps tasks (validate user input, verify assumptions, verify results, monitor infrastructure health) to validation types. Arrows show the corresponding types: Variable validation, Precondition, Postcondition, and Check.
Summary
  • Variable validation runs first during planning and prevents bad inputs from progressing.
  • Preconditions run after planning but before resource creation; they block incompatible attempts.
  • Postconditions run after resource creation/update to assert the desired state; they block when assertions fail.
  • Check blocks run last and emit warnings; they are useful for non-blocking, ongoing checks.
Remember the sequence: variable validation → preconditions → postconditions → checks. Choosing the right validation type ensures you catch the right problems at the right time.
Links and references

Watch Video