Variable Validation : Cross-referential inputs

Terraform version: v0.13.0

I am trying to leverage the new variable validation functionality to validate an input variable based on another input variable, ‘amount’ which represents what the reserved count variable gets set to universally across all modules called in my particular deployment implementation:

variable "amount"{
  default = 1

variable "domain-mappings" {
  type = list(object({
    http_c2_domain = string
  default = [
      http_c2_domain =""
  validation {
      length(var.domain-mappings != var.amount)
    error_message = "The amount of domain mappings must equal the amount of HTTP C2 deployments."

Given the error indictating that it is not currently supported:

Error: Invalid reference in variable validation

  on line 54, in variable "domain-mappings":
  54:       length(var.domain-mappings != var.amount)

The condition for variable "domain-mappings" can only refer to the variable
itself, using var.domain-mappings.

Is there any other way to accomplish my goal of applying the count / amount validation? I tried hacking it in through local, with no luck.

Thanks so much for any assistance.

Hi @joeminicucci,

At the moment, I think the best way to do that would be to combine these two related values into a single variable of object type and then apply the validation rule to that object.

variable "domains" {
  type = object({
    amount = number
    mappings = list(object({
      http_c2_domain = string

  validation {
    condition     = == length(
    error_message = "The number of given domain mappings must match the \"amount\" value."

This will work for my use case! It would be cool to have the ability to reference other variables but I understand there are constraints in terms of how the dependency graphs are instantiated. The reason its a nice-to-have is make the variable names look cleaner, i.e. not prefixing “domains.” before “amount” references. Nevertheless, thank you for the solution :slightly_smiling_face:


You can also use a precondition on a specific resource or data object in your module, which uses the same syntax as validation, but has access to all vars.