concept
Input validation and custom conditions
Input validation enforces caller constraints; preconditions test resource assumptions and postconditions test results. Failed blocking conditions do not undo earlier operations.
Formal Syntax & Structure
terraform syntaxterraform
Input validation and custom conditionsWorking Example
Example Snippetterraform
variable "capacity" { type = number default = 4 nullable = false}
variable "replicas" { type = number default = 2 nullable = false
validation { condition = var.replicas >= 1 && var.replicas <= var.capacity && floor(var.replicas) == var.replicas error_message = "Replicas must be a positive whole number no greater than capacity." }}
output "replicas" { value = var.replicas}