I am a huge fan of and avid user of the module_variable_optional_attrs
optional parameter for custom objects. After using it for a while, I believe it would be better to omit optional object keys if theyre not passed explicitly instead of including the object with a null value.
Here is a use case. I have a variable type object:
variable "cells_definition" {
description = "Nested map where the key is a region you want to enable and keys referring to resource arns to enable. Services enabled: `elasticloadbalancing`, `autoscaling`, `lambda`. Example below:"
type = map(object({
elasticloadbalancing = optional(string)
autoscaling = optional(string)
lambda = optional(string)
}))
}
The value might look like this:
cells_definition = {
us-west-2 = {
elasticloadbalancing = "arn:aws:elasticloadbalancing:us-west-2:<>:loadbalancer/app/<>"
autoscaling = "arn:aws:autoscaling:us-west-2:<>:autoScalingGroup:*:autoScalingGroupName/<>
}
us-east-1 = {
elasticloadbalancing = "arn:aws:elasticloadbalancing:us-east-1:<>:loadbalancer/app/<>"
autoscaling = "arn:aws:autoscaling:us-east-1:<>:autoScalingGroup:*:autoScalingGroupName/<>"
}
}
In my module I attempt to construct a list of all the services passed (the keys) with:
service_list = setintersection(flatten([for k, v in var.cells_definition : keys(v)]))
and the output looks like:
setintersection(flatten([for k, v in var.cells_definition : keys(v)]))
toset([
"autoscaling",
"elasticloadbalancing",
"lambda",
])
Lambda is included even though its not passed in cell_definitions
because its included as a nulled value in each iterable:
setintersection(flatten([for k, v in var.cells_definition : v]))
toset([
{
"autoscaling" = <>
"elasticloadbalancing" = <>
"lambda" = tostring(null)
},
Anyways, just my 2 cents. It would be better to omit the value