Introduction
This procedure outlines how to remove a resource from Terraform's state file using the removed block in your configuration, while leaving the real infrastructure resource intact. This is useful when you want to stop managing a resource with Terraform but do not wish to destroy it.
Prerequisites (if applicable)
- Terraform v1.7 or later is required to use the `removed` block.
- VCS driven workflows in terraform cloud and terraform enterprise.
Overview
In Terraform, when you delete a resource block from your configuration, Terraform will typically plan to destroy the actual infrastructure resource that was being managed by that block.
However, there are scenarios where you may want to stop managing a resource with Terraform without destroying the infrastructure it manages. The `removed` block serves this function: it allows you to remove a resource from Terraform’s state, while ensuring the real infrastructure object remains intact. This way, the resource is no longer tracked by Terraform, but it is not deleted from your cloud environment.
Procedures
Steps to Remove a Resource from Terraform State
1. Identify the Resource to Be Removed
Before starting, identify the resource that you want to remove from Terraform's management but keep in the real infrastructure. Note the resource's Terraform address (e.g., aws_instance.example).
2. Modify the Terraform Configuration
Remove the resource block from your existing Terraform configuration and replace it with the removed block. This block tells Terraform to stop managing the resource without destroying it
removed {
from = null_resource.null
lifecycle {
destroy = false
}
}
Explanation:
- from: Specifies the address of the resource you are removing (e.g., aws_instance.example).
- lifecycle: This block is required and controls Terraform's behaviour:
- destroy = false: Ensures that the resource is removed from Terraform’s state but the actual infrastructure remains untouched.
3. Apply the Configuration
Run the following Terraform commands to update the state:
-
-
-
Initialize Terraform (if not already done):
terraform init
- Plan the Changes:
terraform plan
Output :
Terraform will perform the following actions:
# null_resource.null will no longer be managed by Terraform, but will not be destroyed
# (destroy = false is set in the configuration)
. resource "null_resource" "null2331" {
id = "1169332259171977768"
} - Apply the changes :
terraform apply
Output :
Terraform will perform the following actions:
# null_resource.null will no longer be managed by Terraform, but will not be destroyed
# (destroy = false is set in the configuration)
. resource "null_resource" "null2331" {
id = "1169332259171977768"
} - Verify the Resource Removal :
- Use terraform state list and confirm that the resource is no longer in the list.
- Select Workspace in TFE/TFC > Overview > Resource.
-
-
Additional Information:
https://developer.hashicorp.com/terraform/language/resources/syntax#removing-resources