Search code examples
terraformdatasourceterraform0.12+

Terraform : Data source retrieves only from one source


I have a Network Load Balancer resource ->

resource "network_load_balancer_network_load_balancer" "nlb" {
count = 2
  .
  .
}

and a data source for the same ->

data "network_load_balancer_network_load_balancer" "nlb" {
  depends_on               = [network_load_balancer_network_load_balancer.nlb]
  count                    = 2
  network_load_balancer_id = oci_network_load_balancer_network_load_balancer.nlb[count.index].id

}

locals {
  nlb_ip_addresses = [for nlb in data.oci_network_load_balancer_network_load_balancer.nlb: nlb.ip_addresses[0].ip_address ]
}

Now, currently I have one NLB already in my environment, so by the count 2 , it should create another NLB that is being planned in this terraform config.

Environment - 1 NLB

Current Plan- 1 NLB

In this scenario , on terraform plan , datasource is returning only 1 resource , and local variable nlb_ip_addresses has only one element in it and not 2.

Is this expected behaviour ? Is the approach right ? Does data source not consider the resource being planned in the current terraform config ?

There has been same usage of data source in the code currently (without count - ie. only single nlb) and it had been working fine , so just wondering where I'm going wrong.


Solution

  • If you have an existing NLB which you want to "add" to TF you have to import it so that it can be managed by TF.

    Data sources are not used for that. import operation is independent from them.