I’m working with a Terraform state file that was created a couple of years ago. Since then, a lot of manual changes have been made in the AWS. As a result, we have a huge Terraform drift.
Today, when I ran terraform plan
, I noticed that one of the EC2 instances was flagged for recreation. Terraform couldn’t find the instance it was tracking, since it had been deleted manually. However, I saw that a new instance with the same name already exists in AWS.
It turns out that someone had manually deleted the original instance and created a new one to replace it without using Terraform.
What can I do?
I am new to Terraform and I am afraid of messing it all up...
Will this solve my issue?
terraform state rm module.ec2-instance.aws_instance.instance
terraform import module.ec2-instance.aws_instance.instance i-0011aa22334b455c6
Or will it destroy the existing instance?
So, terraform state rm
followed by terraform import
worked for me:
terraform state rm module.ec2-instance.aws_instance.instance
terraform import module.ec2-instance.aws_instance.instance i-0011aa22334b455c6
Important context:
The original instance had already been destroyed manually (i.e., no longer existed in AWS).
The replacement instance was created manually, but now it’s properly tracked by Terraform.