I’m using Terraform v0.12.29 and have the following within variables.tf:
variable "cors_rule" {
type = any
default = [
{
allowed_methods = ["PUT", "POST"]
allowed_origins = ["https://test1.google.com", "https://test2.google.com"]
allowed_headers = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3600
},
{
allowed_methods = ["HEAD", "GET"]
allowed_origins = ["https://test1.google.com"]
allowed_headers = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}
]
// allowed_origins
validation {
condition = can(tolist([
for origin in lookup(var.cors_rule, "allowed_origins", []) : regex("^.*google.com", origin)
]))
error_message = "Attribute 'allowed_origins' of 'cors_rule' variable url must end with yahoo.com."
}
// max_age_seconds
validation {
condition = tonumber(lookup(var.cors_rule, "max_age_seconds", 0)) =< 3600
error_message = "Attribute 'max_age_seconds' of 'cors_rule' must be less or equal to 3600 seconds."
}
}
Yet the validation condition is failing for me with the following output. How do I handle maps within validation condition?
Error: Invalid value for variable
on variables.tf line 161:
161: variable "cors_rule" {
Attribute 'allowed_origins' of 'cors_rule' variable url must end with google.com.
This was checked by the validation rule at variables.tf:198,3-13.
Error: Invalid function argument
on variables.tf line 207, in variable "cors_rule":
207: condition = tonumber(lookup(var.cors_rule, "max_age_seconds", 0)) >= 3600
|----------------
| var.cors_rule is tuple with 2 elements
Invalid value for "inputMap" parameter: lookup() requires a map as the first
argument.