Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depends_on for data_source #10603

Closed
samber opened this issue Dec 8, 2016 · 5 comments · Fixed by #10670
Closed

depends_on for data_source #10603

samber opened this issue Dec 8, 2016 · 5 comments · Fixed by #10670
Assignees

Comments

@samber
Copy link
Contributor

samber commented Dec 8, 2016

Hi guys,

I am using the depends_on parameter on data-source.

provider "consul" {
    address       = "consul:8500"
    datacenter    = "dc1"
}

resource "consul_key_prefix" "job_config" {
    datacenter = "dc1"
    path_prefix = "services/api/nomad-job/"

    subkeys = {
        port=4242
    }
}

data "consul_keys" "read_env" {
    datacenter = "dc1"
    depends_on = ["consul_key_prefix.job_config"]

    key {
        name = "port"
        path = "services/api/nomad-job/port"
    }
}

output "vars" { value = "${data.consul_keys.read_env}" }

It needs to be applied 2 times to get the right output.

@apparentlymart
Copy link
Contributor

Hi @samber. Sorry for the issue here.

I think I remember seeing this issue on here before but I couldn't find it quickly. IIRC the problem here is that Terraform uses computed attributes to decide whether to defer a data source from refresh time to apply time, but an explicit depends_on does not create such a computed attribute and so Terraform thinks it can refresh the data source early.

You're right that this is a bug, but for now I would suggest working around it by adding a "dummy interpolation" to your data resource so that Terraform knows it must defer loading it, if possible. This means finding a computed attribute from the resource to interpolate somewhere into the data resource. I assume your example here is just a contrived repro case, but there's usually something you can interpolate to force Terraform to do the right thing here.

The real fix in the long run would be for the code that decides whether to defer a data source to also check the explicit depends_on, though we'd probably need to make some assumptions here to get useful behavior. At first blush it feels like the right behavior would be to look at all of the depends_on items and verify that they all have a "primary" resource instance in the state. If they don't then we defer, but if we do then we can assume that the resource attributes would've been updated during the refresh pass and so the right value should already be available.

@samber
Copy link
Contributor Author

samber commented Dec 8, 2016

Ok, thanks for your fast reply. We will find a work around with a faked computed attribute call.

@jbardin
Copy link
Member

jbardin commented Dec 12, 2016

@apparentlymart,

I think we can get by with the simpler solution here, and just check for depends_on values (PR submitted). There will only be entries there if the user specifically added them to the config. Since there's no other reason to add depends_on for a data source, I think it should be safe to assume that the user wanted to enforce a "happens before" relationship.

The only issue I can think of with this, is somehow breaking a config where depends_on was needlessly added, and left in because it had no side-effects.

jbardin added a commit that referenced this issue Dec 12, 2016
Prevent data sources from being aplied early
richardbowden added a commit to richardbowden/terraform-provider-aws that referenced this issue Feb 25, 2018
this addes the ability to search for EIP’s via tags or other attribute that EIP’s can use in a filter.

This did highlight a known issue with regrads to how terraform eval’s data sources before they should if be the resourse is a computed value. This is only an issue if the data block is referencing a resource that is created at the same time. If referencing a pre exiting resource this does not happen.

the test config testAccDataSourceAwsEipFilterConfig makes a interpol ref to force terraform to eval the data block after the resource has either been created or read in `values = ["${aws_eip.test.tags.Name}”]` see the following links

hashicorp/terraform#10603
hashicorp/terraform#17173
@sanchetanparmar
Copy link

I am facing same issue on latest version

Terraform v0.12.6
+ provider.azurerm v1.32.1

Basic Terraform Code

data "azurerm_network_interface" "myterraformnic" {
  name                = "myNIC"
  resource_group_name = "myResourceGroup"
  depends_on = ["azurerm_network_interface.myterraformnic"]
}

output "private_ip_address" {
  value = "${azurerm_network_interface.myterraformnic.private_ip_address}"
}
 
variable "ip_type" {
  default = "Dynamic"
}

resource "azurerm_network_interface" "myterraformnic" {
    name                = "myNIC"
    location            = "eastus"
    resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
    network_security_group_id = "${azurerm_network_security_group.myterraformnsg.id}"

    lifecycle {
            prevent_destroy = true
    }

    ip_configuration {
        name                          = "myNicConfiguration"
        subnet_id                     = "${azurerm_subnet.myterraformsubnet.id}"
        private_ip_address_allocation =   "Dynamic"

    }
}

If depends_on works for data it can solve Azure Static ip issue as well.

@ghost
Copy link

ghost commented Aug 22, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Aug 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants