Dependency on function calls

What is the advised approach when a function call has dependencies on another resource ?

e.g:

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 module.network.lan_ids : {
        lanID             = tonumber(clanid)
        loadbalancerID    = module.loadbalancer.ids[i]
      }
      ])
    })
  ]
}

on this example, networks will be empty because the object network doesn’t exist yet.
depends_on will not work as function calls are resolved earlier.

what is the design, method, suggestion for overcoming this situation ?

Hi @wmsan,

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?

Hi @jbardin,

in the root module, there is as usual a group of modules to build my infrastructure, followed by

module "network" {
  source        = "./modules/network"
  datacenter_id = data.datacenter.selected.id
}

module "connection" {
  source = "./modules/service-connection"
  lan_ids = module.network.lan_ids
  loadbalancer_ids = module.network.loadbalancer.ids
}

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.

What would you suggest ?

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.

It was a typo. I understood your point and functionality. I could also reproduce as expected. Everything works fine and the answer is modules.

Thanks for the support!