# Terraform learned to say what it assumed

> Terraform 1.2 added precondition and postcondition blocks. Nothing in them was impossible before with a clever variable validation and a lot of swearing, but they put the check where the assumption actually lives.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-06-18
- Tags: Terraform, Infrastructure as Code, AWS, CI/CD
- URL: https://deyanpeev.com/posts/terraform-learned-to-say-what-it-assumed

Terraform 1.2 came out in the middle of May, and the feature I've used every
week since is the least exciting one in the announcement, the `precondition`
and `postcondition` blocks.

They enable nothing new. What they change is where you're allowed to write
something down, and that has mattered more to me than a new capability would.

# The failure they are for

Every module I have written has assumptions in it that aren't expressed
anywhere. The AMI passed in is expected to be x86. The subnet is expected to be
private, and the instance type has to support the number of network interfaces
the rest of the configuration attaches to it.

When one of those is wrong, Terraform tells you something else is wrong, forty
lines into an apply, and in the provider's words, not the assumption's. The AWS
API refuses a call, you read an error about an unsupported parameter
combination, and you spend twenty minutes working backwards to the input that
caused it.

Variable validation, added back in 0.13, covers part of this. But it only sees
the variable, so it can check a string against a regex and that's about it. The
assumptions that break are about relationships between values, and half of them
involve data that Terraform only learns by looking something up.

# A precondition and a postcondition

A precondition sits on the thing that holds the assumption:

```hcl
data "aws_ami" "app" {
  most_recent = true
  owners      = ["self"]

  filter {
    name   = "name"
    values = [var.ami_name]
  }
}

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

  lifecycle {
    precondition {
      condition     = data.aws_ami.app.architecture == "x86_64"
      error_message = "The selected AMI is ${data.aws_ami.app.architecture}; this instance type needs x86_64."
    }
  }
}
```

The error arrives during plan, it names the assumption, and it interpolates the
value that violated it (error messages became full expressions in this release,
and that's why they are worth reading now).

A postcondition checks the result instead of the input. It's the one I use
with data sources:

```hcl
data "aws_subnet" "chosen" {
  id = var.subnet_id

  lifecycle {
    postcondition {
      condition     = self.map_public_ip_on_launch == false
      error_message = "Subnet ${var.subnet_id} assigns public IPs; this module expects a private subnet."
    }
  }
}
```

`self` inside a postcondition refers to the object being checked. The pattern I
have settled on is that preconditions guard what a resource is about to be
given, and postconditions assert what a lookup came back with.

# Why the location matters

All of this was possible before. You could contort a `validation` block, or add a
`null_resource` with a `count` that fails to evaluate, or write the check in the
pipeline. People did all three, and I've written two of them.

What you couldn't do was put the assumption next to the resource that holds
it. An assumption written in a pipeline step is documentation, and it drifts.
Written in the `lifecycle` block of the resource it constrains, it gets read by
the next person at the moment they need it, because they're already looking at
that resource. They are editing it.

It's the same argument as for a type annotation or a test name, I think. The
check matters less than the fact that it can't end up in the wrong place.

# Not policy, and not the provider's job

Not for policy. "No public S3 buckets in this account" is an organisational
rule and not a module assumption, and it belongs somewhere that applies whether
or not a particular module was used. Preconditions live inside the code they
guard, and that is the wrong place for a rule meant to constrain code that
hasn't been written yet.

And not as a substitute for the provider's own validation. If the API will reject
it clearly, let it. The checks worth writing are for failures that would
otherwise surface somewhere that doesn't mention the cause.

The other addition in 1.2 I have found a use for is `replace_triggered_by`,
which replaces a resource when an upstream dependency changes. It's a tidier
answer than the hash-in-a-tag trick I had been using to force replacements.
