Hello friends, I am trying to consolidate some variables to make a module easier to use, and found myself in a pickle.
I have something like:
variable "stateful_config" {
type = object({
per_instance_config = map(object({
#name is the key
stateful_disks = map(object({
#device_name is the key
mode = string
}))
}))
mig_config = object({
stateful_disks = map(object({
#device_name is the key
mode = string
}))
})
default = null
}
So an example would be:
{
per_instance_config = {
instance_1 = {
stateful_disks = {
persistent-disk-1 = {
mode = 'READ_ONLY'
}
}
}
}
mig_config = {
stateful_disks = {
persistent-disk-1 = {
mode = 'READ_ONLY'
}
}
}
}
This is because GCP provider allows you to configure stateful managed instance groups and define disk parameters for the entire group and/or per specific instance of the group.
Here is the rundown:
- User can define stateful_config with 0-n per_instance_configs,
- Additionally, they can define 0-1 mig_config,
- Finally, both per_instance_configs and mig_config allow 0-n stateful_disks
Considering this, if I use a conditional for_each I get the expected error that this is an object so will only be known after apply:
...
resource "google_compute_instance_group_manager" "default" {
...
dynamic "stateful_disk" {
for_each = var.stateful_config && var.stateful_config.mig_config != null ? {} : var.stateful_config.mig_config.stateful_disks
iterator = config
content {
device_name = config.key
delete_rule = config.value.delete_rule
}
}
...
}
...
# and google_compute_per_instance_config is another can of worms, we can focus just on the above.
Error:
E │ var.stateful_config is a object, known only after apply
E
E Unsuitable value for left operand: bool required.
E
E Error: Invalid operand
Thoughts or suggestions?