0
0
Terraformcloud~10 mins

Preconditions and postconditions in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Preconditions and postconditions
Start Terraform Apply
Check Preconditions
Stop with Error
Create/Update Resources
Check Postconditions
Stop with Error
Finish Apply Successfully
Terraform first checks preconditions before creating resources. After creation, it checks postconditions to ensure success.
Execution Sample
Terraform
terraform {
  required_version = ">= 1.3"
}

resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Hello"
  }

  lifecycle {
    precondition {
      condition     = length("Hello") > 0
      error_message = "String must not be empty"
    }
    postcondition {
      condition     = true
      error_message = "Postcondition failed"
    }
  }
}
This Terraform resource uses precondition to check a string is not empty before creation, and a postcondition that always passes after creation.
Process Table
StepActionCondition CheckedResultNext Step
1Start terraform applyN/AN/ACheck preconditions
2Check precondition: length("Hello") > 0length("Hello") > 0TrueCreate resource
3Create resource null_resource.exampleN/AResource createdCheck postconditions
4Check postcondition: truetrueTrueFinish apply
5Finish terraform applyN/ASuccessEnd
💡 All preconditions and postconditions passed, apply finishes successfully.
Status Tracker
VariableStartAfter PreconditionAfter Resource CreationAfter PostconditionFinal
length("Hello")N/A5555
precondition resultN/ATrueTrueTrueTrue
postcondition resultN/AN/AN/ATrueTrue
resource stateNot createdNot createdCreatedCreatedCreated
Key Moments - 2 Insights
Why does Terraform stop if a precondition fails?
Terraform stops immediately on a failed precondition to prevent creating resources with invalid inputs, as shown in execution_table step 2 where a false condition would stop the process.
What happens if a postcondition fails after resource creation?
Terraform stops the apply with an error after resource creation if a postcondition fails, preventing a false success state, as indicated in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the precondition check at step 2?
AError
BTrue
CFalse
DNot evaluated
💡 Hint
Check the 'Result' column in row for step 2 in execution_table.
At which step does Terraform create the resource?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the 'Create resource' action in execution_table.
If the precondition was false, what would happen?
ATerraform would continue to create the resource
BTerraform would ignore the precondition and check postcondition
CTerraform would stop with an error before creating the resource
DTerraform would skip postcondition checks
💡 Hint
Refer to the concept_flow where 'No' from precondition leads to stopping with error.
Concept Snapshot
Terraform Preconditions and Postconditions:
- Preconditions run before resource creation to validate inputs.
- Postconditions run after creation to verify success.
- If any condition fails, Terraform stops apply with an error.
- Use lifecycle blocks with precondition and postcondition.
- Helps catch errors early and ensure resource correctness.
Full Transcript
Terraform applies resources by first checking preconditions to ensure inputs are valid. If preconditions fail, Terraform stops and shows an error to avoid creating bad resources. If preconditions pass, Terraform creates or updates the resource. After creation, postconditions run to verify the resource state is as expected. If postconditions fail, Terraform stops with an error to avoid false success. This process ensures safe and correct infrastructure deployment.