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.