In current Terraform, how does a variable validation block differ from a resource precondition in terms of when its check is evaluated?
variable "instance_count" {
type = number
validation {
condition = var.instance_count > 0
error_message = "The instance_count must be greater than zero."
}
}- ABoth are evaluated only during apply, so neither can stop an invalid input from reaching the plan phase.
- BVariable validation is checked as the input value is assigned, before most other evaluation, whereas a precondition is checked during plan and apply once the resource it guards is being evaluated. Correct
- CVariable validation runs after the resource is created so it can inspect computed attributes, while a precondition runs before any input is read.
- DBoth run at exactly the same point, and the only difference is that validation can reference other resources while a precondition cannot.
Why A is wrong: This is tempting because both are custom conditions, but validation runs early on the input and preconditions are checked during plan as well as apply, not apply alone.
Why B is correct: A validation block tests the input variable itself and fails early on assignment, while a precondition is tied to a resource or output and runs when that object is evaluated during plan and apply.
Why C is wrong: The ordering is reversed: validation cannot see computed attributes because it runs on the raw input, and a precondition does not run before inputs are read.
Why D is wrong: They do not run at the same point, and validation is actually the more restricted of the two because it may reference only the variable itself, not other resources.