Delete State for Resources Managed by Terraform

July 04, 2023

terraformlogo

Today we will use a very powerfull CLI tool that can delete a state of the resources managed by terraform

Deleting a state of the resources managed by terraform can be very usefull thing, however it's important to note that deleting the Terraform state for specific resources should be done with caution, as it can have significant implications on the managed infrastructure. Always ensure that you have a clear understanding of the impact and take appropriate backup measures if necessary.

First of all i would like to highlight usecases when it can be usefull:

  • Resource Cleanup: Deleting the Terraform state for specific resources allows you to clean up those resources that are no longer needed. This can be useful when you want to remove specific infrastructure components or resources from your environment. By deleting the state, you ensure that Terraform no longer manages those resources, and subsequent Terraform operations will not attempt to modify or destroy them.

  • State Corruption: In some cases, the Terraform state file may become corrupted or out of sync with the actual infrastructure. This can occur due to manual modifications made to the infrastructure outside of Terraform or other factors. When state corruption occurs, certain resources may be in an inconsistent state or have incorrect information stored in the state file. By deleting the state for those resources, you can start fresh and allow Terraform to recreate the state accurately.

  • Infrastructure Reorganization: As your infrastructure evolves, you may decide to reorganize or refactor certain resources or modules. In such cases, it may be necessary to delete the Terraform state for those resources to align them with the new infrastructure structure. By deleting the state, you can redefine the resources in your Terraform configuration, apply the changes, and let Terraform recreate them according to the updated configuration.

  • Partial Resource Management: There may be scenarios where you want to manage a subset of resources using Terraform while leaving other resources untouched. In such cases, you can delete the state for the resources you wish to manage with Terraform, effectively removing them from Terraform's management scope. This allows you to have fine-grained control over which resources are managed by Terraform and which ones are managed through other means.

  • Development Iteration: During the development and testing phase, you may need to iterate on certain resources frequently. Deleting the state for those specific resources enables you to make changes to the Terraform configuration and reapply them without affecting other resources. It allows you to isolate the changes and iterate faster during the development process.

Prerequisites:

  1. Install Go - here
  2. Install Terraform - here
  3. Install fuzzy-terraform-rm:
    • run git clone https://github.com/paololazzari/fuzzy-terraform-rm
    • run cd fuzzy-terraform-rm
    • run go build -o /usr/local/bin/

As example i will use my terraform code to create 6 resources:

aws_default_vpc.default
aws_sns_topic.alarm
aws_instance.my_instance
aws_security_group.demo_sg
aws_cloudwatch_metric_alarm.instance-health-check
aws_cloudwatch_metric_alarm.cpu-utilization

terraform-apply

Now i would like to remove state for ec2 instance and make it no longer managed with terraform:

In order to do that i would use fuzzy-terraform-rm command and hit enter on aws_instance.my_instance resource: rminstance

Note: if you would need to remove state for few resources use tab to mark them for deletion

  • Type y to confirm. You will see output that it is removed:

removed

So for now i have only t2.micro ec2 instance running on my AWS account, which is no longer managed by terraform: ec2t2

Let's say we would need to use now t3.micro instance in our setup. Let's update our terraform code and plan to see the output:

t3plan As you can see terraform is going to create a new ec2 instance and update other connected resources in order to use newly created instance while normally terraform would destroy our previous instance and crete new one. After running terraform apply we can see in AWS Console that second instance started creating: 2instances

Our first instance remains untouched - terraform is not managing it, as expected. In order to delete it we should do it manually now