Lesson 2 of 3 Custom Conditions and Checks

Preconditions and Postconditions

Preconditions and postconditions check assumptions and guarantees around individual resources, data sources and outputs. This lesson covers where they go, when they run and how to choose between them.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish preconditions from postconditions.
  • Choose where to place a custom condition.

Preconditions

A precondition block goes inside the lifecycle block of a resource or data source, or directly inside an output block. Terraform evaluates it before planning or applying that object, so it checks the assumptions the object relies on.

data "aws_ami" "app" {
  most_recent = true
  owners      = ["self"]
  filter {
    name   = "name"
    values = ["app-*"]
  }
}

resource "aws_instance" "app" {
  ami           = data.aws_ami.app.id
  instance_type = var.instance_type

  lifecycle {
    precondition {
      condition     = data.aws_ami.app.architecture == "x86_64"
      error_message = "The selected AMI must use the x86_64 architecture."
    }
  }
}

Here the instance type is an x86 type, so the instance assumes that the image found by the data source is x86. If someone publishes an ARM image that matches the filter, the precondition stops the plan with a clear message instead of a launch failure.

Postconditions

A postcondition block goes inside the lifecycle block of a resource or data source. Terraform evaluates it after planning or applying the object, so it checks the guarantees the object must provide to the rest of the configuration. It can use self to refer to the object's attributes.

resource "aws_instance" "web" {
  ami           = data.aws_ami.app.id
  instance_type = "t3.micro"

  lifecycle {
    postcondition {
      condition     = self.public_dns != ""
      error_message = "The instance must have a public DNS name; check the subnet settings."
    }
  }
}

Output blocks support preconditions but not postconditions. When a condition depends on values unknown until apply, Terraform evaluates it during apply. A failed precondition or postcondition is an error: the run stops, and objects that depend on the failed object are not planned or changed.

precondition
EvaluatedBefore the object is planned or applied
ChecksAssumptions the object relies on
Can use selfNo
Allowed inResources, data sources, outputs
postcondition
EvaluatedAfter the object is planned or applied
ChecksGuarantees the object provides
Can use selfYes
Allowed inResources, data sources
True or false

Pre- and postconditions

Exam skills 4.7.2

Check where preconditions and postconditions can be used.

Log in to try this activity Interactive activities are free with an account.

Choosing where to put a condition

HashiCorp's guidance is to place each condition where it best documents the intent. Validate a single input with a variable validation. Check an assumption about data the configuration reads with a precondition on the resource that uses it, or a postcondition on the data source. Check a promise a module makes to its callers with a postcondition on the resource or a precondition on the output that exposes it.

  • Variable validation: is this input value acceptable?
  • Precondition: is it safe to create or use this object, given its inputs?
  • Postcondition: did this object turn out as the rest of the configuration needs?
  • Output precondition: is this value valid to hand to callers?

The following diagram shows where a module checks a guarantee it makes to callers.

Where the network module checks its two-zone guarantee. A precondition on the output that exposes the private subnet IDs stops the run inside the module with a clear message when the subnets span fewer than two Availability Zones; a variable validation on the zone list fails even earlier. Without either, the caller's database deployment fails much later.
The condition is illustrative.
Sort into groups

Which kind of condition?

Exam skills 4.7.1 · 4.7.2 · 4.7.3

Choose the mechanism that best documents each rule.

Log in to try this activity Interactive activities are free with an account.
Worked example

A guarantee to callers

A network module promises that its private subnets span at least two Availability Zones, and callers build highly available databases on them. A caller passes one zone, and their database deployment fails much later. Where should the module check this?

The decisive point is that the requirement is a guarantee the module gives.

A precondition on the output that exposes the subnet IDs, such as condition = length(distinct(values(aws_subnet.private)[*].availability_zone)) >= 2, stops the run inside the network module with a clear message. A variable validation on the zone list would also work and fails even earlier; using both documents the rule for maintainers and callers.

Check yourself

Which kind of condition can refer to the object itself with self?

Show answer

A postcondition, because it is evaluated after the object is planned or applied.

Exam Tips

Remember for the exam

  • Preconditions check assumptions before; postconditions check guarantees after.
  • Both live in lifecycle blocks; outputs take preconditions only.
  • self works in postconditions.
  • Failures are errors that stop the run.