I’m working on a way to remove all the null values from complex object list variable. The only place it’s failing to work as expected is on the configs() object where the loop is not removing the null values. Samples below to provide some context.
variables.tf
variable "instances" {
type = list(object({
env_code = string,
env_number = number,
enable_ssl = optional(bool, false),
server_groups = list(object({
number_of_instances = number,
web_function = string,
configs = optional(object({
location = string
cluster = string
connect_retry_secs = optional(string)
}))
}))
}))
main.tf
{ instances = [for instance in var.instances : merge(
{ for k, v in instance : k => v if v != null },
{ server_groups = [for server_group in instance.server_groups : merge(
{ for k, v in server_group : k => v if v != null },
server_group.configs == null ? {} :
{ for k, v in server_group.configs : k => v if v != null },
)] },
)] },
Snippet of plan output
+ configs = {
+ connect_retry_secs = null
+ location = "/location"
+ set_handler = null
+ cluster = "localhost1 localhost2"
}
I added in the server_group.configs == null ? {} :
condition because TF was not happy abut attempting to access the k/v’s of a null object. That has me fairly certain that server_group.configs is actually accessing the correct object, but for some reason { for k, v in server_group.configs : k => v if v != null }
is not iterating over the object’s attributes.
I feel like I’m close but I just can’t quite figure it out. Any assistance would be much appreciated!