How to remove a resource from Terraform state?
To remove a resource from the Terraform state, you can use the $ terraform state rm command. This command allows you to remove resources from the Terraform state file.
1. How to remove a single resource from terraform state file?
Here are the steps which you should follow before you remove any resource -
- List the resource- Using the following terraform list resource command first you can check how many resources you are planning to remove from the state file.
1# List all the resources
2
3terraform state list
- Remove the resource - If you want to remove a resource named
my_resource
from the state, you can use the following command:
1# Remove the resource name - my_resource
2
3terraform state rm my_resource
- $terraform apply - Previous command will remove the resource and all of its associated data from the state file. You can then use the $terraform apply command to apply the changes and remove the resource from your infrastructure.
It's important to note that removing a resource from the Terraform state does not destroy the resource itself. If you want to destroy the resource and remove it from your infrastructure, you will need to use the $terraform destroy command.
2. How to remove multiple resources from terraform state file?
There is no out-of-the-box support for removing multiple resources from the state file. But you can be a little smart with you shell scripting skills and can build short custom commands to remove multiple resources from the state file.
Here is my first example -
1# Instead of aws_instance, you can supply your own resource name
2
3terraform state rm $(terraform state list | grep aws_instance)
Here is my second example -
1# Remove multiple resources without locking the state between each resource
2
3terraform state list | cut -f 1 -d '[' | xargs -L 1 terraform state rm
3. How to remove the s3 bucket/ec2 instance from the terrafrom state file?
If you want to remove an S3 bucket named my-bucket from the state, you can use the following command:
1# Remove S3 bucket from state file
2# Replace the my_bucket name with the correct bucket name.
3
4 terraform state rm aws_s3_bucket.my-bucket
1# Remove EC2 instance from the state file
2# Replace the my_bucket name with correct EC2 instance name.
3
4 terraform state rm aws_instance.my-instance
This will remove the S3 bucket and all of its associated data from the state file. You can then use the terraform apply
command to apply the changes and remove the bucket from your infrastructure.
It's important to note that removing an S3 bucket from the Terraform state does not delete the bucket itself.
4. How to save the backup of terraform state file while using terraform state rm?
To use the $ terraform state rm command with the -backup flag, you can specify the path where you want to save a backup of the state file before the changes are applied.
Here is an example of using the $ terraform state rm command with the -backup flag to remove an EC2 instance from the state and save a backup of the state file:
1# Specify the path to the backup state file before removing the resource
2
3terraform state rm -backup=./state_backups/aws_instance.my-instance aws_instance.my-instance
4
In this example, the EC2 instance named my-instance will be removed from the state file, and a backup of the state file will be saved to the ./state_backups/ directory with the name aws_instance.my-instance.
You can use the -backup flag to save a backup of the state file before making any changes, in case you need to restore the state later on.
For more information about the terraform state rm command and how to use it, you can refer to the Terraform documentation
5. Use -path flag to specify the path of the state file to modify the resource
To use the $ terraform state rm command with the -state flag, you can specify the path to the state file that you want to modify.
Here is an example of using the $ terraform state rm command with the -state flag to remove an EC2 instance from a state file located in a different directory:
1# Here you can specify the state file that you want to modify
2
3terraform state rm -state=./my-project/terraform.tfstate aws_instance.my-instance
In this example, the EC2 instance named my-instance will be removed from the state file located at ./my-project/terraform.tfstate.
Posts in this Series
- Securing Sensitive Data in Terraform
- Boost Your AWS Security with Terraform : A Step-by-Step Guide
- How to Load Input Data from a File in Terraform?
- Can Terraform be used to provision on-premises infrastructure?
- Fixing the Terraform Error creating IAM Role. MalformedPolicyDocument Has prohibited field Resource
- In terraform how to handle null value with default value?
- Terraform use module output variables as inputs for another module?
- How to Reference a Resource Created by a Terraform Module?
- Understanding Terraform Escape Sequences
- How to fix private-dns-enabled cannot be set because there is already a conflicting DNS domain?
- Use Terraform to manage AWS IAM Policies, Roles and Users
- How to split Your Terraform main.tf File into Multiple Files
- How to use Terraform variable within variable
- Mastering the Terraform Lookup Function for Dynamic Keys
- Copy files to EC2 and S3 bucket using Terraform
- Troubleshooting Error creating EC2 Subnet InvalidSubnet Range The CIDR is Invalid
- Troubleshooting InvalidParameter Security group and subnet belong to different networks
- Managing strings in Terraform: A comprehensive guide
- How to use terraform depends_on meta argument?
- What is user_data in Terraform?
- Why you should not store terraform state file(.tfstate) inside Git Repository?
- How to import existing resource using terraform import comand?
- Terraform - A detailed guide on setting up ALB(Application Load Balancer) and SSL?
- Testing Infrastructure as Code with Terraform?
- How to remove a resource from Terraform state?
- What is Terraform null Resource?
- In terraform how to skip creation of resource if the resource already exist?
- How to setup Virtual machine on Google Cloud Platform
- How to use Terraform locals?
- Terraform Guide - Docker Containers & AWS ECR(elastic container registry)?
- How to generate SSH key in Terraform using tls_private_key?
- How to fix-Terraform Error acquiring the state lock ConditionalCheckFiledException?
- Terraform Template - A complete guide?
- How to use Terragrunt?
- Terraform and AWS Multi account Setup?
- Terraform and AWS credentials handling?
- How to fix-error configuring S3 Backend no valid credential sources for S3 Backend found?
- Terraform state locking using DynamoDB (aws_dynamodb_table)?
- Managing Terraform states?
- Securing AWS secrets using HashiCorp Vault with Terraform?
- How to use Workspaces in Terraform?
- How to run specific terraform resource, module, target?
- How Terraform modules works?
- Secure AWS EC2s & GCP VMs with Terraform SSH Keys!
- What is terraform provisioner?
- Is terraform destroy needed before terraform apply?
- How to fix terraform error Your query returned no results. Please change your search criteria and try again?
- How to use Terraform Data sources?
- How to use Terraform resource meta arguments?
- How to use Terraform Dynamic blocks?
- Terraform - How to nuke AWS resources and save additional AWS infrastructure cost?
- Understanding terraform count, for_each and for loop?
- How to use Terraform output values?
- How to fix error configuring Terraform AWS Provider error validating provider credentials error calling sts GetCallerIdentity SignatureDoesNotMatch?
- How to fix Invalid function argument on line in provider credentials file google Invalid value for path parameter no file exists
- How to fix error value for undeclared variable a variable named was assigned on the command line?
- What is variable.tf and terraform.tfvars?
- How to use Terraform Variables - Locals,Input,Output
- Terraform create EC2 Instance on AWS
- How to fix Error creating service account googleapi Error 403 Identity and Access Management (IAM) API has not been used in project before or it is disabled
- Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11