-
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
The evaluating of data source working with count is wrong (probably due to early evaluating) #19349
Comments
Hi @ckyoog! Sorry for this strange behavior and thank you for investigating it so deeply. I think you are right that this bug is caused by the evaluation of data resources early during the "refresh" phase, rather than during the "plan" phase where resources are processed. This seems like a specific example of the general problems I was describing in #17034, where evaluating the data resource separately from everything else means that Terraform is often evaluating them in an complete environment, where resources have not yet been processed. In most cases this just results in using the old values for resource attributes in expressions, but in the situation you've found here it causes a new instance to not be available yet at all, which leads to downstream errors. The good news is that I think the ideas discussed in #17034 will address this problem too, and your thorough reproduction cases here will be very helpful in testing that new behavior once we are ready to implement it in a future release. Thanks again for the awesome investigation work here! |
Hi @apparentlymart Thank you for your comment. I'm glad this is a known issue, and you have already had proposal to fix it. Hope terraform gets better and better. |
Hi guys, just wondering if this is the same error described here: I got an error while adding additional
The certificate should be updated before the lookup of the route53 records are evaluated. Right now with v0.11.11 it just fails:
I've tried with taint:
But no luck, got the same error. I've also tried creating a intermediate local variables:
But still doesn't work. Is this the same bug described here? Any advice on how to workaround this problem? |
@aserrallerios , it is not same bug. It is not even bug in your problem. It doesn't work not because of bug, but because of your misuse. Read more document please. |
This has since been fixed in more recent versions of terraform - thanks everyone! If you think there is still a bug in v0.13 (that there isn't already an issue open for) or you would like to make a feature request, please open a new issue and fill out the template. |
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. |
Terraform Version
Description of the issue
The code I am going to show you is a demo code, not a real code used in practice. I simplify the code to make the bug easy to see. And It is a complete code, you can run it on you local machine.
First, let's see an ordinary code.
There is no code more ordinary than this. We have two resources (to simplify it, I use
null_resource
here), and one refers to (depends on) another. This code is the base of my whole talking.After running
terraform apply
, we create two resources which follow our cfgs defined incfg
.Then, we add a new cfg
{ k = "0" }
at the top ofcfg
, likeThere is no any problems to run
terraform plan
regarding the newcfg
. Everything works just fine.Introduce intermediate object. Introduce error.
As the code grows, somehow, we are gonna need an intermediate object between resource
a
andb
. That isb
->intermediate
->a
. Like below,(Here I use data source of
template_file
as intermediate object. It is an usual option for intermediate object before data source ofnull_data_source
is available. However, the both data source have the same bug I am talking now, you can trynull_data_source
yourself. Cuz I've tried both of them myself)Add a new cfg,
{ k = "0" }
, like we did above, then runterraform plan
, this time we will get error.An interesting workaround
In my practice, I found a workaround using local variable.
Add a new cfg,
{ k = "0" }
, like we did above, then runterraform plan
, this time everything goes well again.By adding a looks-meaningless local variable, the out-of-bound error disappears. Interesting. (Maybe the local variable defers the evaluating of
template_file
.)Here is the plan output after new cfg
{ k = "0"}
is added. I will compare to this output later.A little deeper thinking. No-need-computed attribute
I noticed that the
template_file.intermediate
refers toid
ofnull_resource.a
, which needs to be computed. It might be the reason why the out-of-bound error occurs. Becausenull_resource.a[2]
has not existed yet when evaluatingdata.template_file.intermediate
. Theid
ofnull_resource.a[2]
can't be computed, so terraform would probably ignoredata.template_file.intermediate[2]
. According to these thinking, I made such changes as below,(This time, instead,
data.template_file.intermediate
refers totriggers.k
ofnull_resource.a
, of which the value doesn't need to be computed, because the value can be established as long as terraform finishes reading the configuration file, no need to do any real things, like creating a resource.)Add a new cfg,
{ k = "0" }
, like we did above, then runterraform plan
, this time everything goes well as well.No errors. Looks good. But wait! Please review the plan output carefully. Do you find anything wrong?
Yes. The output shows that the values of
triggers.k
ofnull_resource.b[0-2]
are1
,2
,2
, which should be0
,1
,2
. The value oftrigger.k
ofnull_resource.b
comes fromdata.template_file.intermediate
, so obviously thedata.template_file.intermediate
still holds the old value, does not get updated.This is easy to be understood, I think. Because compared to the last "workaround" output, this output misses the part below, which is regarding the data source
intermediate
.Sum up
intermediate
refers to resourcea
directly. (Out-of-bound error)intermediate
refers to no-need-computedattribute
of resourcea
.The text was updated successfully, but these errors were encountered: