How to fix Error: Precondition failed in Terraform

TerraformINTERMEDIATEMEDIUMUpdated Dec 30, 2025· 152 views

A precondition failed error occurs when a lifecycle precondition block evaluates to false in Terraform 1.2+. Fix it by reviewing the condition logic, input variables, and ensuring assumptions are met before applying.

Quick fix

Fixes Error: Precondition failed

Error: Resource precondition failed

  on main.tf line 42, in resource "aws_instance" "example":
   42:   precondition {

condition = contains(["us-east-1", "us-west-2"], var.aws_region)
var.aws_region is "eu-west-1"

Your message: AWS instance can only be created in us-east-1 or us-west-2
  1. Read the full error message carefully
  2. Verify input variables and their values
  3. Review the precondition logic
  4. Adjust your configuration to meet the precondition
terraform — error
Error: Resource precondition failed
on main.tf line 42, in resource "aws_instance" "example":
42: precondition {
condition = contains(["us-east-1", "us-west-2"], var.aws_region)
var.aws_region is "eu-west-1"
Your message: AWS instance can only be created in us-east-1 or us-west-2
Error: Precondition failed

What this error means

In Terraform 1.2 and later, you can add preconditions and postconditions to resource, data source, and output blocks via the lifecycle meta-argument. A precondition is a validation check that Terraform evaluates after generating a plan but before creating the resource. When a precondition's condition expression evaluates to false, Terraform halts the operation and displays the error_message you defined. This is Terraform's way of enforcing assumptions about your infrastructure configuration—ensuring certain conditions are true before proceeding. Unlike variable validation (which runs immediately on input), preconditions run later in the planning phase and can reference other configuration objects, making them powerful for cross-validation.

Typical symptoms

  • terraform apply or terraform plan fails with 'Error: Resource precondition failed' or similar
  • The error message includes a custom message explaining why the condition failed
  • The specific resource, data source, or output name is shown in the error
  • The failure prevents any changes from being applied to your infrastructure

Common causes

  1. The condition expression in your precondition block evaluates to false due to current variable or resource state
  2. Input variable values do not match the conditions your precondition is checking
  3. Resource or data source attributes referenced by the condition are not in the expected state
  4. The condition logic contains an error (incorrect operators, missing properties, wrong function calls)
  5. Your infrastructure assumptions have changed since the configuration was written

How to fix "Error: Precondition failed"

1Read the full error message carefully

When a precondition fails, Terraform displays the error_message you defined in the precondition block. This message should explain why the condition failed.

bash
Error: Resource precondition failed

  on main.tf line 42, in resource "aws_instance" "example":
   42:   precondition {

condition = contains(["us-east-1", "us-west-2"], var.aws_region)
var.aws_region is "eu-west-1"

Your message: AWS instance can only be created in us-east-1 or us-west-2

The error shows:
- Which file and line the precondition is on
- The condition expression that failed
- The actual values involved
- Your custom error_message

Study this output to understand what assumption was violated.

2Verify input variables and their values

Review the variables referenced in your precondition. Use terraform console or print statements to check their actual values:

bash
terraform console
> var.aws_region
"eu-west-1"
> var.environment
"production"

Or check your terraform.tfvars or environment variables:

bash
# Check what variables are being passed
cat terraform.tfvars

# Check environment variables
env | grep TF_VAR

Ensure the values passed match what your precondition expects. If using automation, verify that the correct variables are being provided.

3Review the precondition logic

Check your precondition block in the resource definition. Verify the condition expression is correct:

hcl
resource "aws_instance" "example" {
  # ... configuration ...

  lifecycle {
    precondition {
      condition     = can(data.aws_ami.ubuntu.id)  # Ensure data source exists
      error_message = "Ubuntu AMI data source lookup failed."
    }
  }
}

Common mistakes:
- Using wrong operators (== instead of contains, != instead of !=)
- Referencing properties that don't exist on the object
- Using self (not allowed in preconditions—use resource references instead)
- Logic errors in boolean expressions

Test the condition expression independently using 'terraform console' if needed:

bash
terraform console
> contains(["us-east-1", "us-west-2"], var.aws_region)
true  # or false, depending on the variable
4Adjust your configuration to meet the precondition

Once you understand why the precondition failed, fix it by either:

Option 1: Update the input variables
Update your terraform.tfvars or pass different values:

bash
# terraform.tfvars
aws_region = "us-east-1"  # Change from eu-west-1
environment = "production"

Or via CLI:

bash
terraform apply -var="aws_region=us-east-1"

Option 2: Modify the precondition logic
If the condition is too restrictive, update it in your configuration:

hcl
lifecycle {
  precondition {
    # Add eu-west-1 to allowed regions
    condition     = contains(["us-east-1", "us-west-2", "eu-west-1"], var.aws_region)
    error_message = "AWS instance can only be created in specified regions."
  }
}

Option 3: Check data source prerequisites
If the precondition checks a data source (like looking up an AMI), ensure that data source can resolve:

bash
terraform plan  # Verify data sources fetch successfully
terraform console
> data.aws_ami.ubuntu.id  # Check if it resolves
5Replan and apply after fixing

Once you've adjusted your configuration:

bash
# Generate a new plan to verify the precondition now passes
terraform plan

# If the plan looks good, apply the changes
terraform apply

Terraform will evaluate the precondition again. If it passes, the plan will proceed to creating/modifying resources. If it still fails, you'll get the same error and can iterate further.

Advanced notes

Preconditions vs Postconditions:
- Preconditions run before resource creation (after planning). They validate assumptions.
- Postconditions run after resource creation. They validate guarantees the resource must meet.

Preconditions vs Variable Validation:
- Variable validation runs immediately when variables are evaluated, cannot reference other configuration objects.
- Preconditions run during the planning phase and can reference resources, data sources, and outputs.

Where Preconditions Can Be Used:
- Resource blocks: lifecycle { precondition { ... } }
- Data source blocks: lifecycle { precondition { ... } }
- Output blocks: lifecycle { precondition { ... } }

Important Limitations:
- Cannot use 'self' in preconditions (not defined until after plan)
- Preconditions must reference at least one object from elsewhere in configuration
- If multiple preconditions are defined, all must pass; Terraform reports all failures

Terraform Version:
Preconditions and postconditions require Terraform 1.2.0 or later. If you're on an older version, upgrade to use this feature.

Frequently asked questions

What does "Error: Precondition failed" mean?
2 and later, you can add preconditions and postconditions to resource, data source, and output blocks via the lifecycle meta-argument. A precondition is a validation check that Terraform evaluates after generating a plan but before creating the resource. When a precondition's condition expression evaluates to false, Terraform halts the operation and displays the error_message you defined.
How do I fix "Error: Precondition failed"?
A precondition failed error occurs when a lifecycle precondition block evaluates to false in Terraform 1.2+. Fix it by reviewing the condition logic, input variables, and ensuring assumptions are met before applying.
What causes "Error: Precondition failed"?
The condition expression in your precondition block evaluates to false due to current variable or resource state Input variable values do not match the conditions your precondition is checking Resource or data source attributes referenced by the condition are not in the expected state
Is the "Error: Precondition failed" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading