How to set dependency based on existence of a variable in terraform?

I tried setting up dependency using a variable existence but getting syntax error.

│ Error: Invalid expression
│   on line 231, in resource "kubernetes_persistent_volume_claim" "prometheus-pvc":
│  231:   depends_on = [ ? kubernetes_config_map.aws_auth : null]
│ A single static variable reference is required: only attribute access and
│ indexing with constant keys. No calculations, function calls, template
│ expressions, etc are allowed here.

Any suggestion how to implement that?

Dependencies in Terraform are static, and used to determine how the configuration is evaluated. You cannot conditionally depend on something, because that dependency is there to determine when to evaluate the expression in the first place. In other words, arbitrary expressions are not allowed, only because they would have no effect on the order of evaluation.

In your example, you can list both references in depends_on .

depends_on = [, kubernetes_config_map.aws_auth]

we want to use that dependency part to work only if the is defined.

say is not defined then the dependency should be.

depends_on =

if the is defined, then the depends should be.
depends_on = [kubernetes_config_map.aws_auth]

So, your suggestion, doesn’t work here
depends_on = [, kubernetes_config_map.aws_auth]

Names and references in Terraform are again always static. This means there is no way to write or kubernetes_config_map.aws_auth anywhere in the configuration if they are not defined. It’s possible that those values are null an have no impact on the configuration, and in that case the depends_on references make no difference.

depends_on is only giving Terraform information about which order to evaluate the configuration, but once you are evaluating the configuration that dependency information has already been taken into account.

I added the into the depends_on to try and make a point about how it’s just the static references which matter, but if actually the dependency is only kubernetes_config_map.aws_auth, that would be all you need in the list.

this also failed, got the same error

Hi @ukreddy-erwin,

The suggested configuration cannot cause the same error, because it would consist only of static references. Please show exactly what the configuration looks like, and what the error output is.

Since the original question was about using a configuration which isn’t possible, and could not produce a different result, it may help if you describe the problem you are actually trying to solve.