Error: Invalid index... resource.name is an empty tuple - Failure in plan on destroy

Hi,

I’m on the tf 0.12.25-
I have a conditional resource which has a count of only 0 or 1.
It’s referenced in other resources using ‘attr = resource.name[0].id’

resource.name has gone away in real life but still exists in state and I’m trying to destroy the entire project but now I fail in plan with:
Error: Invalid index… resource.name is an empty tuple

I feel like this should not break a plan in destroy, maybe I’m wrong?
Is this a bug and if not, what is the recommended fix?
TIA

Hi @yruss972,

Although Terraform does make a best effort to try to synchronize the state with the remote objects prior to creating a plan, it does not always succeed. Deleting something Terraform was managing outside Terraform is a situation that Terraform can often detect in isolation, but that can create problems in a broader configuration because you’re asking Terraform to use the id of something that no longer exists to configure some other object.

The usual way to go here is to let Terraform be the one to delete something, rather than deleting it outside of Terraform. However, if something is mistakenly deleted outside of Terraform and Terraform is unable to work around that mistake then you can give Terraform a hint as to what you did by telling it explicitly to “forget” the object that no longer exists:

terraform state rm resource.name

This will directly edit the state to no longer include resource.name, and so when you next run terraform plan Terraform will behave as if this were an entirely new object to be created. As long as the configuration still contains the resource block for that object and the count evaluates to 1, Terraform will then understand that your intent is to create a new remote object to represent that.

So generally, we expect Terraform to do all the destroying and the resource going away was not intentional.
Our Terraform automation doesn’t allow users to directly interact with the state or perform state manipulations so this required an admin to manually intervene.
I understand how the resource missing could affect a terraform apply and how that might need to be handled by state manipulation.
I don’t understand, however, why it’s a problem in the destroy.
All the resources are about to be destroyed and so we have one less to take care of?