terraformterraform-stateterraform-import

terraform state rm & terraform import


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?


Solution

  • 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: