Ravi
Ravi

Reputation: 342

How to delete infrastructure resources if we loose tfstate file in terraform?

I created a terraform template to deploy EC2 VM. It also creates subnet, security groups, network interfaces required to create EC2 VM. I uploaded this terraform template to the S3 bucket. Next, I created the Lambda function that downloads this terraform template and creates resources. I don't have the tfstate file as the Lambda function is terminated. Now, I want to destroy these resources created by the Lambda function using Terraform template.

How can I do that?

I have tried the following things but didn't work:

  1. I have a terraform plan file in the local environment. Didn't find a way to pass this to terraform destroy.

  2. When I tried this template from my local environment, it generated a tfstate file. I used that file to destroy resources but got the following error: Terraform detected the following changes made outside of terraform since the last terraform apply.

  3. I tried terraform refresh and terraform delete to refresh the state of resources and destroy them.

  4. I tried terraform plan -destroy and terraform apply -destroy commands as well.

Is there any other way to delete these resources other than manually?

Upvotes: 3

Views: 3307

Answers (1)

Mohammed Almusaddar
Mohammed Almusaddar

Reputation: 797

Since you have the terraform resources and templates you cloud also import resources manually into state file using terraform import, but test the upper workflow first.

Upvotes: 1

Related Questions