Interview Prep/Terraform/Resources Variables and Expressions

Your AMI must be x86_64 and the plan should fail otherwise. How do you enforce that inside Terraform?

IntermediateScenarioResources Variables and Expressions
Answer

Situation read → Catch wrong assumptions at plan/apply time rather than at boot.

What I'd do → Add a precondition or postcondition.

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

  lifecycle {
    postcondition {
      condition     = self.architecture == "x86_64"
      error_message = "AMI ${self.id} is ${self.architecture}, expected x86_64."
    }
  }
}

Why → Conditions encode assumptions where the data is read, with a message that tells the operator exactly what is wrong.

Interview tip → Contrast with check blocks (1.5+), which warn but don't block.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions