I am trying to add a set of annotations based on a variable value.
So far, I am trying to add something like:
annotations = merge({
annotation1 = value
annotation2 = value
try(var.omitAnnotations == false) == null ? {} : {
annotation3 = value
annotation4 = value
}})
However, this doesn't appear to be working as expected, and in fact, the annotations are always added, regardless of the value in var.addAnnotations
How can I get this logic working, so that annotation3 & annotation4 are only added when var.addAnnotations is true?
Thanks in advance!
I would suggest moving any expressions that are too complicated to a local variable, because of visibility:
What this will do is set the
local.annotations
variable to an empty map ifvar.omitAnnotations
istrue
or to a map with annotationsannotation3
andannotation4
defined ifvar.omitAnnotations
isfalse
. The local variableadd_annotations
will then have the value assigned based on using thetry
built-in function [1]. In the last step, you would just do the following: