Terraform error: The "count" value depends on resource attributes

Hi,

I have the following issue. When I want to run the code below, I have the error message: The “count” value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on.
The tf code creates a complete private hdinsight kafka cluster, but because wrong design, I had to create the DNS entries manually for the cluster.

The problematic line is the count line from the following code:

data "azurerm_lb" "loadBalancers" {
  depends_on = [ data.azurerm_resources.loadBalancer-resources ]
  resource_group_name = var.resource_group
  count = length(data.azurerm_resources.loadBalancer-resources.resources)
  name = data.azurerm_resources.loadBalancer-resources.resources[count.index].name
}

According to terraform, it cannot determine the count value without apply. That’s fine, but the count value should be evaluated after the deployment of dependent resources got finished.

Full tf code:

module "hdinsight-storage" {
  source = "../azure-storage-account"
  add-pvep = false
  log-analytics-id = var.log-analytics-id
  private_dns_zone_id = var.storage-dns-zone-id
  subnet_id = var.storage-subnet-id
  subresource-names = [ "blob" ]
  access-tier = "Hot"
  account-kind = "StorageV2"
  public-network-access-enabled = true
  resource_group = var.resource_group
  location = var.location
  tier = "Standard"
  name = replace("st${var.name}","-","")
  replication_type = "ZRS"
}

resource "time_sleep" "wait_30_s" {
  depends_on = [ module.hdinsight-storage ]
  create_duration = "30s"
}

module "hdinsight-container" {
  source = "../azure-storage-container"
  depends_on = [ time_sleep.wait_30_s ]
  name = "hdinsight"
  storage_account_name = module.hdinsight-storage.storage-name
}

resource "azurerm_hdinsight_kafka_cluster" "kafka" {
  depends_on = [ module.hdinsight-container ]
  name = var.name
  location = var.location
  resource_group_name = var.resource_group
  cluster_version = var.cluster-version
  tier = var.cluster-tier
  
  gateway {
    username = var.gw-username
    password = var.gw-password
  }

  storage_account {
    storage_container_id = module.hdinsight-container.container-id
    storage_account_key = module.hdinsight-storage.storage-key
    is_default = true
    storage_resource_id = module.hdinsight-storage.storage-id
  }

  component_version {
    kafka = var.kafka-component-version
  }

  network {
    connection_direction = "Outbound"
    private_link_enabled = true
  }

  roles {
    head_node {
      password = var.head-node-password
      vm_size = var.head-node-vm-size
      username = var.node-username
      subnet_id = var.kafka-subnet-id
      virtual_network_id = var.kafka-vnet-id
    }

    worker_node {
      password = var.worker-node-password
      vm_size = var.worker-node-vm-size
      username = var.node-username
      number_of_disks_per_node = var.worker-node-disks-per-node
      target_instance_count = var.worker-node-instance
      subnet_id = var.kafka-subnet-id
      virtual_network_id = var.kafka-vnet-id
    }

    zookeeper_node {
      username = var.node-username
      password = var.zookeeper-node-password
      vm_size = var.zookeeper-node-vm-size
      subnet_id = var.kafka-subnet-id
      virtual_network_id = var.kafka-vnet-id
    }
  }
}

data "azurerm_resources" "loadBalancer-resources" {
  depends_on = [ azurerm_hdinsight_kafka_cluster.kafka ]
  resource_group_name = var.resource_group
  type = "Microsoft.Network/loadBalancers"
}

data "azurerm_lb" "loadBalancers" {
  depends_on = [ data.azurerm_resources.loadBalancer-resources ]
  resource_group_name = var.resource_group
  count = length(data.azurerm_resources.loadBalancer-resources.resources)
  name = data.azurerm_resources.loadBalancer-resources.resources[count.index].name
}

resource "azurerm_private_dns_a_record" "kafka_records" {
  depends_on = [ data.azurerm_lb.loadBalancers ]
  count = length(data.azurerm_lb.loadBalancers)
  name                = startswith(data.azurerm_lb.loadBalancers[count.index].name,"gateway") ? "${var.name}" : "${var.name}-ssh"
  zone_name           = var.kafka-dns-zone
  resource_group_name = var.kafka-dns-zone-rg
  provider            = azurerm.connectivity
  ttl                 = 300
  records             = [ data.azurerm_lb.loadBalancers[count.index].frontend_ip_configuration[0].private_ip_address ]
}

output "storage-id" {
  value = module.hdinsight-storage.storage-id
}

output "blob-id" {
  value = module.hdinsight-container.container-id
}

output "kafka-id" {
  value = azurerm_hdinsight_kafka_cluster.kafka.id
}

output "kafka-proxy-endpoint" {
  value = azurerm_hdinsight_kafka_cluster.kafka.kafka_rest_proxy_endpoint
}

output "kafka-https-endpoint" {
  value = azurerm_hdinsight_kafka_cluster.kafka.https_endpoint
}

output "kafka-gateway" {
  value = azurerm_hdinsight_kafka_cluster.kafka.gateway
}

I can give value 2 to count, but it is not a good option…
Thanks!
G