validating a list of input variables in terraform

Viewed 1873

This validation block works for a single input variable.

variable "mytestname" {

     validation {
        condition = length(regexall("test$", var.mytestname)) > 0
        error_message = "Should end in test"
     }
}

I need it to work inside a for_each - or have some workaround to accomplish this. The issue is that there is a restriction on the condition statement - the condition HAS to take in the input variable itself (i.e. - it cannot accept an each.value)

variable "mytestnames" {

listnames = split(",",var.mytestnames)     

for_each = var.listnames

     validation {
        condition = length(regexall("test$", each.value)) > 0
        error_message = "Should end in test"
      }
}

The above snippet does not work. I need a way I can iterate over a list of values and validate each of them. It looks like the newly introduced 'validation block' does not work on lists of input variables. There must be a way to do this without a validation block...??

1 Answers

I believe it will not work. The attributes that can be defined in the variable block are type, description, and default. So how could we define additional attribute such as "listnames" dynamically...

variable "mytestnames" {
    listnames = split(",",var.mytestnames)
}
$ terraform validate

Error: Unsupported argument

  on hoge.tf line 3, in variable "mytestnames":
   3: listnames = split(",",var.mytestnames)

An argument named "listnames" is not expected here.

Related