Terraform doesn't recognize resources after VM restore

Hi everyone,

I’m hitting a particular case in which I am not sure how to proceed:
We use Terraform to manage VMs in a VMware vSphere-based datacenter.

Recently, there was an issue with some of those VMs we manage with Terraform, which resulted in the data center admins restoring the VMs from a Backup.
The VMs are up and running again, but whenever we want to plan/apply our unchanged Terraform recipes now, Terraform does not recognize these VMs anymore and suggests creating new ones.
Since other resources depend on these VMs, we can’t really skip these.

How to recover from that? How can we tell Terraform that the existing VM XYZ is the one from its statefile?

Best regards,
Marc

You will need to use the terraform state rm command to have Terraform forget about the VMs that no longer exist, and the terraform import command to reassociate the restored VMs with the Terraform resource addresses that would otherwise be created.

Having done so, review terraform plan carefully to be certain everything is as expected before running an apply.