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

template_file resource "primary" ends up null in tfstate file #2939

Closed
chiefy opened this issue Aug 5, 2015 · 11 comments
Closed

template_file resource "primary" ends up null in tfstate file #2939

chiefy opened this issue Aug 5, 2015 · 11 comments

Comments

@chiefy
Copy link

chiefy commented Aug 5, 2015

We are using TF in a team setting, and one of my co-workers ended up getting his tfstate file like this yesterday:

template is null

Unfortunately, I have no idea how he did this, but it took me about an hour to figure out because literally every TF command I tried ended up spitting out

Resource 'template_file.user-data-script' does not have attribute 'rendered' for variable 'template_file.user-data-script.rendered'

The solution was to go in and manually edit the .tfstate file, I moved an old deposed template resource into the primary and everything started working again.

Again, I cannot reproduce, but just wanted to document this happening in case it pops up again.

@ketzacoatl
Copy link
Contributor

I have run into this quite a few times, and I'm not sure how to reproduce. I have fixed/resolved the issue with manual tweaks to the state file, though I do not (right now) remember what those updates were.

@ketzacoatl
Copy link
Contributor

@phinze / @mitchellh, Could this be a bug with resource dependency resolution? It seems as though it is.

I have a template_file resource which is provided as a variable (input string) to a module, and which ends up being user_data for instances created. If I provide the module with user_data = "foobar", instead of user_data = "${template_file.leaders-init.rendered}", I can successfully target the template:

ᐅ terraform plan -target=template_file.leaders-init
Refreshing Terraform state prior to plan...

The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ template_file.leaders-init
    filename:              "" => "scripts/init-leaders.tpl"
    rendered:              "" => "<computed>"
    vars.#:                "" => "10"
    ...

Plan: 1 to add, 0 to change, 0 to destroy.

...when I leave the "${template_file.leaders-init.rendered}", Terraform gets stuck:

ᐅ terraform plan -target=template_file.leaders-init
Error configuring: 1 error(s) occurred:

* Resource 'template_file.leaders-init' does not have attribute 'rendered' for variable 'template_file.leaders-init.rendered'

Carefully using the knowledge noted above, particularly with -target=template_file.foo, I can iterate over the list of templates with terraform plan and apply so as to add each template_file resource back into the terraform.tfstate, and then carry on with my regular use of terraform.

@zoltrain
Copy link

zoltrain commented Sep 7, 2015

I've also just run into this issue, it does seem related to resource dependencies when using raw resources, who's values are passed to modules.

I get it when trying to create a Route 53 DNS record (I have a DNS module for reuse) that depends on ELB endpoint value from a resource that's not a module. It seems the only way to guarantee that the right order of creation is either, make everything a resource in a flat way (no modules) and explicitly handle the depends, or make EVERYTHING a module. Is depends_on going to add module -> resource, resource -> module bindings in the future?

@phinze
Copy link
Contributor

phinze commented Sep 8, 2015

This definitely has the feel of a core bug. Tagged as such.

If anybody managed to get this narrowed down to a repro case please do share! In the meantime I'll keep an eye out for it in personal TF usage and report back if I can reproduce.

@phinze
Copy link
Contributor

phinze commented Sep 8, 2015

Looks like #3182 has a reproducible relevant scenario which should help us get some debugging done. 👍

@ketzacoatl
Copy link
Contributor

@phinze I would share more code, but it is a bit buried in code that is not yet open-sourced. I will see if I can reproduce the bug with one of my modules that could be more easily shared. Glad to see we have some traction either way!

@ketzacoatl
Copy link
Contributor

@phinze, would it help if I spent the time to get some code online for you, based on what I am doing? Looking over the code in the referenced issue, I might be doing something functionally the same within Terraform (to trip the same bug), but my code and logic is different. In short: I have a bunch of TF modules which accept a user_data variable, and which I often supply as a rendered template. So the template is outside the module and passed in, the stuff that uses the template is inside the module.

@ketzacoatl
Copy link
Contributor

On a related note, when I issue a terraform destroy -force in this environment, the destroy succeeds, nothing is left at AWS.. but TF throws a bunch of errors, related to the template file:

aws_vpc.my_app: Destroying...
aws_vpc.my_app: Destruction complete
Error applying plan:

14 error(s) occurred:

* Resource 'template_file.leaders-init' does not have attribute 'rendered' for variable 'template_file.leaders-init.rendered'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_vpc.my_app' does not have attribute 'id' for variable 'aws_vpc.my_app.id'
* Resource 'aws_security_group.a-app' does not have attribute 'id' for variable 'aws_security_group.a-app.id'
* Resource 'aws_security_group.b-app' does not have attribute 'id' for variable 'aws_security_group.b-app.id'
* Resource 'aws_security_group.q-inbound' does not have attribute 'id' for variable 'aws_security_group.q-inbound.id'
* Resource 'aws_route_table.my_app' does not have attribute 'id' for variable 'aws_route_table.my_app.id'
* Resource 'aws_route_table.my_app' does not have attribute 'id' for variable 'aws_route_table.my_app.id'
* Resource 'aws_route_table.my_app' does not have attribute 'id' for variable 'aws_route_table.my_app.id'
* Resource 'aws_route_table.my_app' does not have attribute 'id' for variable 'aws_route_table.my_app.id'

@daguava
Copy link

daguava commented Aug 11, 2016

Ran into this issue today, after a successful deploy, the primary field had been written out to file as null, had to substitute in an old deposed section of json.

@mitchellh
Copy link
Contributor

#3182 is fixed which we felt this was related to, and there are other newer issues that have simiar error messages. Because there is no clean repro here and its been so long I'm going to close this issue and move on to the newer ones. If you see it again please open an issue and please try to get a repro!

Note that "lost state" would be a top top priority issue in any case so any repro will be prioritized. At this point there we aren't hearing of these issues.

@ghost
Copy link

ghost commented Apr 21, 2020

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 Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants