Depends_on having issues

Hello,
Please help me to fix this issue.

I have the following code, which is not working.

depends_on = [var.instance_name[count.index] == backend ? backend : null]

below the error which throwing :

Error: Invalid expression

│ on azurevm.tf line 48, in resource “azurerm_virtual_machine” “main”:
│ 48: depends_on = var.instance_name[count.index] == backend ? backend : null

│ A static list expression is required.

Terraform is telling you that it does not allow you to use count.index in a depends_on.

Dependencies are between entire blocks - it doesn’t track separate dependencies for e.g. item [0] and [1] of a block.

@PBSankar,

In order to evaluate an expression, Terraform first must connect all dependencies to have the data for evaluation. This means that if an expression like your depends_on example were allowed, the dependencies would have already been connected before the result is even known, making the result useless which is why it’s not allowed. I would assume var.instance_name is already taken into account in the resource config, and the backend data is coming from another expression, so this depends_on probably has no effect whatsoever.