Skip to content

Variable validation should allow checking other variables #24374

@alexjurkiewicz

Description

@alexjurkiewicz

Sometimes I have variables like shut_down_on_weekends:

variable environment_type {
  type = string
  description = "test or prod"
}

variable shut_down_on_weekends {
  type = bool
  description = "You can't shut production environments down on the weekend"
  default = false
}

locals {
  actually_shut_down = var.shut_down_on_weekends && var.environment_type == "test"
}

Rather than creating an intermediate local to silently fix the issue, I would prefer to use variable validation to raise an error at planning time:

variable shut_down_on_weekends {
  type = bool
  validation {
    condition     = var.shut_down_on_weekends || var.environment_type == "test"
    error_message = "You can only shut test environments down on the weekend."
  }
}

It would be possible to write cyclic dependencies with this functionality, so some care would need to be made to ensure that case is detected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    custom-conditionsFeedback on variable validation, preconditions. postconditions, checks, and test assertionsduplicateissue closed because another issue already tracks this problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions