I’m having difficulty locating a technique to add an optional attribute to a child module invocation. The child module is our own and under our control. The idea is that generally the module will be invoked without the attribute:
module "foo" {
source = "../modules/foo"
this = "foo1"
providers = {
aws.one = aws.one
aws.two = aws.two
}
}
But there are times we need to pass an overriding attribute which the module will act upon:
module "foo" {
source = "../modules/foo"
this = "foo1"
optional_attribute = "fee"
providers = {
aws.one = aws.one
aws.two = aws.two
}
}
The child module invocation is shared by symlink to a number of root modules. Obviously we can solve the problem by providing a non-symlinked version in the root module, but it would be great to have the attribute be sent when a value for it is present and skipped when a value for it is absent.
Our initial approach was to use a variable as the source of the optional attribute, but we don’t want to declare an occasionally used root module attribute in every single root module when it’s used very seldom.
However it looks like there is no way to test if a variable exists and act accordingly, e.g. send the module a null that it can test or a value. At least one bit of documentation suggests that such an approach goes against the grain of a variable, as they “must” exist in some form.
We can however possibly get the value out of state, but again have not found a technique.
Here are some of the things we’ve tried and failed with (each represents an approach, we’d only use one):
#optional_attribute = var.optional_attribute
#optional_attribute = try(var.optional_attribute, true) ? var.optional_attribute : null
#optional_attribute = try(var.optional_attribute, null)
#optional_attribute = local.account-map.optional_attribute = null ? "" : local.account-map.optional_attribute
#optional_attribute = can(local.account-map.optional_attribute) ? local.account-map.optional_attribute : null
Any ideas?