-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
always triggers data source read
#11806
Comments
Hi @queeno! Thanks for this issue. I seem to remember this being done intentionally to fix a bug a while ago. There is possibly something more refined we could do here to make this support more cases, but unfortunately I think right now this is working as designed and we don't really have a better strategy in mind. |
Thanks for your very quick response! That makes sense and wouldn't be too worrying, however as I mentioned earlier, when using the data source in an instance's user-data,
If you could do anything to fix this behaviour, it would be hugely appreciated 👍 😄 Have a look:
|
@queeno as a workaround I suggest that you add a That should then make Terraform see the dependency via the interpolation, allowing you to remove the That is assuming that wasn't just a contrived example for the bug report... If it was, I'm sure it's possible to adapt this workaround to whatever resources you are really using... just interpolate anything from the resource you want to depend on into a template variable. Most resources have a reasonable |
Great advice! I have followed your suggestion, removed the explicit dependency and had it all working as intended. I hope this issue can also help others in my same situation, while you fix the actual bug in terraform. Yes, sorry it was a contrived example for the bug report. This is the actual code I am working on: Thanks again for your help! 👍 |
Data sources are always refreshed during |
I agree. The change that @apparentlymart is referring to is #10670 which is only intended to prevent early evaluation when there is an explicit dependency. I think this can be made to work with depends_on. |
Perhaps a suitable compromise would be this: If a Perhaps this is trickier than it seems though, if e.g. the dependencies are indirect via nodes that don't themselves generate diffs (modules, for example). |
I'm just throwing it there to you, but why would the behaviour of an implicit dependency be different from an explicit dependency? In the previous example, if i reference the When I explicitly set the dependency, by using the |
@queeno the problem is that with an interpolation Terraform can tell the difference between the value not being available yet and it being available in the state from a previous run. With My proposed compromise tries to get around that by using the presence of any diff on the dependency as a signal that the data source should be re-run, thus allowing us to mimic the convergent behavior of an interpolation-dependency where it'll trigger the read only when there's a create or update (of any attribute) on the things it depends on. |
@apparentlymart It has to be present in the diff because we need it to be present in the diff for Apply to do anything to downstream nodes that may depend on it. If we don't put the data source in the diff then its outputs won't be computed which won't trigger downstream normal resources to be in the diff and so on... |
I'm also running into this currently because I was trying to use datasources to provide a module<->module dependency in a test framework. I'm setting up a test for a module which has some prereqs but I want it all deployed as 1 project in the test framework. When I add a |
Sorry for the long silence here, everyone! We've been looking at this issue again recently, and I've written up #17034 as a proposal for one way to address it. This proposal builds on the discussion above, and attempts to also deal with some other similar quirks with implicit dependencies from data resources. It'll take some more prototyping to see if that proposal is workable, since there are undoubtedly some subtleties that we didn't consider yet. We won't be able to work on this immediately due to other work in progress, but we do intend to address this. |
I had a similar issue with data source local_file . In general I had null_resource which created file foo, and a data source that was reading the contents of this file in terraform. Whenever I added depends_on the local_file - it always triggered computed content of it. In order to workaround that I used this: resource "null_resource" "create_file" { data "null_data_source" "file" { Later we can reference the data by: ${data.null_data_source.file.outputs["data"]} |
The default service account data resource currently uses a depends_on flag added to prevent a race condition in terraform-google-modules#141 Due to the way that Terraform refreshes data resources, Terraform thinks that the data resource has changed when in actuality it hasn't: hashicorp/terraform#11806 (comment) By changing to use a null data resource that interpolates the default service account email, the data resource will only change when the project number does.
The default service account data resource currently uses a depends_on flag added to prevent a race condition in terraform-google-modules#141 Due to the way that Terraform refreshes data resources, Terraform thinks that the data resource has changed when in actuality it hasn't: hashicorp/terraform#11806 (comment) By changing to use a null data resource that interpolates the default service account email, the data resource will only change when the project number does.
Terraform v0.11.13 I am having the same issue as mentioned here. Unfortunately looks like there is no workaround for my use case at this moment. Or.. any ideas? resource "aws_ecs_task_definition" "application" {
...
}
data "aws_ecs_task_definition" "application" {
task_definition = "${aws_ecs_task_definition.application.family}"
depends_on = ["aws_ecs_task_definition.application"]
}
resource "aws_ecs_service" "application" {
task_definition = "${aws_ecs_task_definition.application.family}:${max("${aws_ecs_task_definition.application.revision}", "${data.aws_ecs_task_definition.application.revision}")}"
...
} Plan:
Output:
|
+1 |
@trebidav if you're reading that task definition immediately after creating it just to get the With that said, the To everyone else: leaving "+1" or 👍 comments here doesn't do anything except create noise for those who are watching this issue for updates. If you want to vote for this issue, please leave a 👍 reaction on the original comment on this issue (not this comment!), since then we can query that as an input for prioritization. |
I am having same issue with latest version. tried with |
I do not know for sure if this is related but when I do not set depends_on in aws_network_interfaces datasource terraform doesn't find anything:
Since I am referring to aws_lb.ec2_service.name it should automatically wait for aws_lb resource but it doesn't for some reason... However if I add depends_on it waits for resource but will trigger updates to dependent resources during each apply... |
Just stumbled over this and just wanted to share my (hacky but not overwhelmingly complex) solution. I basically take the ID of the null_resource and put it into some attribute in the data provider, reducing it to length 0 so it doesn't affect the actual attribute value. Like so:
This should work with any data provider I think. |
Same issue here, Trying to get the aws_route_tables data with a specific filter and it just reads it before creating the resources resulting in: The "count" value depends on resource attributes that cannot be determined |
I have a similar issue too. But the Lambda needs to be invoked only when the secret has been created, so I added Even though there is nothing to create, change, or destroy |
Hi @hfgbarrigas, The behavior you saw there is as intended, because the Although I'd recommend avoiding generating local files on disk if you can, in unusual situations where you can't avoid it you can read the contents of a file using [the resource "time_rotating" "rsa1" {
rotation_minutes = 1
}
resource "null_resource" "create_template_file" {
triggers = {
filename = time_rotating.rsa1.unix
}
provisioner "local-exec" {
# I added this because I assume you're intending to run
# a local command to generate this file. You can refer
# to self.triggers.filename in the provisioner configuration
# to get the filename populated above.
}
}
data "local_file" "generated_template" {
filename = "${path.module}/${null_resource.create_template_file.triggers.filename}"
}
data "template_file" "result" {
template = data.local_file.generated_template.content
vars = {
id = null_resource.rsa1.id
}
} If you have any follow-up questions about that, please feel free to create an issue in the community forum and I can follow up with you there. |
Hi @apparentlymart , you're right regarding the function, completely missed that documentation ... Although, I had tried the snippet you shared and had the same issue when it comes to refresh state and local files. Terraform seems to need the local file when refreshing state even though dependencies have no changes. Is this intended? Here's a snippet to reproduce. data "local_file" "generated_template" {
filename = "${path.module}/${null_resource.file.triggers.timestamp}"
}
data "template_file" "result" {
template = data.local_file.generated_template.content
vars = {
id = null_resource.file.id
}
}
resource "null_resource" "file" {
provisioner "local-exec" {
command = "echo test > $PATH/$NAME"
environment = {
PATH = path.module
NAME = self.triggers.timestamp
}
}
triggers = {
timestamp = time_rotating.test.unix
}
}
resource "time_rotating" "test" {
rotation_minutes = 1
}
output "test" {
value = data.template_file.result.rendered
}
First apply is ok, second apply with file still ok, third apply without file present is not ok. All of these applies were ran under a minute to avoid the time rotation. |
I know this is closed but trying out my luck here with all the great minds out there. I'm trying to set the DNS record of an EMR master instance once its finished creation with the following
However the Thanks in advance for your time! |
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. |
Hi there,
Terraform Version
Terraform v0.8.4
Affected Resource(s)
If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.
Terraform Configuration Files
Debug Output
https://gist.github.com/queeno/f198c93760f7c60e5102e7fd5873ad1f
Expected Behavior
terraform plan
shouldn't re-read the data source.Actual Behavior
terraform plan
re-reads the data source.Steps to Reproduce
terraform plan
Important Factoids
When using
depends_on
intemplate_file
,terraform plan
always seems to re-read the data source. If the data source is used by an instance'suser-data
, terraform plans to change the instance's user-data.terraform apply
, however, doesn't produce any change.If
depends_on
is not used, then the data source is not re-read.The text was updated successfully, but these errors were encountered: