I am trying to validate a map of objects that is typed as below :
variable "ecs_config_map" {
type = map(object({
cpu = number
memory = number
desired = number
capabilities = list(string)
launch_type = string
}))
}
The value for the variable looks like :
ecs_config_map = {
driver = {
cpu = 256
memory = 512
desired = 0
capabilities = ["FARGATE"]
launch_type = "FARGATE"
}
aggregator = {
cpu = 256
memory = 512
desired = 0
capabilities = ["FARGATE"]
launch_type = "FARGATE"
}
}
Now, I want to perform some basic validation, but I cannot seem to get the syntax right.
validation {
condition = contains(["EC2", "FARGATE", "EXTERNAL"], var.ecs_config_map[*]["launch_type"])
error_message = "Only EC2, FARGATE, EXTERNAL are allowed values for launch_type."
}
This threw Invalid value for "list" parameter: list of bool required.
Debugging on terraform console
described :
> type(var.ecs_config_map)
map(
object({
capabilities: list(string),
cpu: number,
desired: number,
launch_type: string,
memory: number,
}),
)
> type(var.ecs_config_map["driver"])
object({
capabilities: list(string),
cpu: number,
desired: number,
launch_type: string,
memory: number,
})
> type(var.ecs_config_map[*])
tuple([
map(
object({
capabilities: list(string),
cpu: number,
desired: number,
launch_type: string,
memory: number,
}),
),
])
which indicated that my problem was because I was trying to iterate over all the objects in the variable. I am using a splat expression when doing var.ecs_config_map[*]
which converts the whole map into a tuple of maps of objects.
Then I tried to use a for
expression to perform the validation
validation {
condition = can(for task in var.ecs_config_map : contains(["EC2", "FARGATE", "EXTERNAL"], task["launch_type"]))
error_message = "Only EC2, FARGATE, EXTERNAL are allowed values for launch_type."
}
And I got thrown The condition for variable "ecs_config_map" can only refer to the variable itself, using var.ecs_config_map.
for_each
did not work either.
Am I messing up the syntax of the validation somewhere? Or am I asking too much from terraform by attempting validation of a complex type?