This portion of your example should work as expected. If module.network or module.loadbalancer is tasked with creating new resources, the outputs will be unknown. This means the networks value will not be empty, it will also be unknown, and resolved during apply.
We would need to see how those module outputs are derived to suggest any other changes. What does the plan output show in this example?
The contents of module/service-connection is basically
resource "helm_release" "my-resource" {
repository = "my-repo/helm"
chart = "my-char"
version = "v0.2.2"
values = [
templatefile("${path.module}/my-resource.tftpl", {
dev = true
datacenter_id = 123
networks = flatten([
for i, clanid in var.lan_ids : {
lanID = tonumber(clanid)
loadbalancerIP = var.loadbalancer_ips[i]
}
])
})
]
}
The error that I have is:
│ Error: Invalid index
│
│ on modules/service-connection/main.tf line 119, in resource “helm_release” “my-resource”:
│ 119: loadbalancerIP = var.loadbalancer_ips[i]
│ ├────────────────
│ │ var.loadbalancer_ips is empty list of string
│
│ The given key does not identify an element in this collection value: the collection has no elements.
So my understanding on that is: Terraform is first resolving the template variables, not taking into consideration the dependency of provisioning first module.network.
I’ve tried using a depends_on but the issue is the same.
The error indicates a variable named loadbalancer_ips while the module call has a an input named loadbalancer_ids, is that a typo in the example?
If module.network.loadbalancer.ids is the correct input for that value, and it’s an empty list when templatefile is evaluated, can you show how that is derived? Assuming the output is generated from resources which are yet to be created, then the value should be unknown which would not result in the Invalid Index error shown.