Step-by-Step Tutorial for Using the removed
Block in Terraform
The removed
block in Terraform (introduced in version 1.7) provides a safe way to remove resources from the Terraform state without destroying them in the real infrastructure. This is especially useful when you want Terraform to stop managing specific resources but retain their existence in your cloud environment.
What is the removed
Block?
The removed
block explicitly informs Terraform that certain resources are no longer managed by the configuration. During terraform apply
, Terraform removes these resources from the state without attempting to delete them in the actual infrastructure.
removed {
from = aws_instance.example
lifecycle {
destroy = false
}
}
When to Use the removed
Block
Here are 10 scenarios where the removed
block is useful:
- Legacy Resource Cleanup: Resources created with Terraform are now managed manually or by another tool.
- Split Configurations: Moving resources from one Terraform workspace/module to another without impacting the infrastructure.
- Orphaned State Entries: Removing unused state entries that no longer exist in real infrastructure.
- Migration to a New Tool: Switching from Terraform to another IaC tool while leaving resources intact.
- Third-Party Resource Management: Transitioning resource management to another team or tool.
- Failed Deletions: When a resource deletion fails but you no longer want Terraform to track it.
- Resource Lock-In Prevention: Detaching resources that must remain after Terraform’s scope changes.
- State File Size Optimization: Removing large numbers of unmanaged resources from the state file to improve performance.
- Custom Manual Adjustments: Detaching resources that are being modified outside Terraform’s scope.
- Testing and Development: Temporarily removing a resource for testing purposes without actually deleting it.
How to Use the removed
Block
Step-by-Step Process
- Identify the Resource to Remove
Useterraform state list
to find the resource in the Terraform state that you want to stop managing.terraform state list
- Add the
removed
Block
Add aremoved
block to your configuration for the resource(s) you want to remove.removed { from = aws_instance.example }
- Run
terraform plan
Terraform will show that the specified resource will be removed from the state.terraform plan
- Apply the Changes
Executeterraform apply
to remove the resource from the state file.terraform apply
- Clean Up the Configuration
After removing the resource from the state, you can remove theremoved
block if it is no longer needed.
5 Code Examples of the removed
Block
Example 1: Removing a Single Resource
# Step 1: Identify the resource
resource "aws_instance" "example" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
# Step 2: Replace the resource block with the removed block
removed {
from = aws_instance.example
}
# Step 3: Run Terraform commands
terraform plan
terraform apply
Example 2: Removing Multiple Resources
# Step 1: Define multiple resources (before removal)
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
resource "aws_instance" "db" {
ami = "ami-87654321"
instance_type = "t3.micro"
}
# Step 2: Add removed blocks for both resources
removed {
from = aws_instance.web
}
removed {
from = aws_instance.db
}
# Step 3: Run Terraform commands
terraform plan
terraform apply
Example 3: Removing Dynamically Created Resources
# Step 1: Original resource definition
resource "aws_s3_bucket" "example" {
for_each = toset(["bucket1", "bucket2", "bucket3"])
bucket = each.key
}
# Step 2: Replace the resource block with removed blocks
removed {
from = aws_s3_bucket.example["bucket1"]
}
removed {
from = aws_s3_bucket.example["bucket2"]
}
removed {
from = aws_s3_bucket.example["bucket3"]
}
# Step 3: Run Terraform commands
terraform plan
terraform apply
Example 4: Transitioning to a New Module
# Step 1: Original resource definition
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
# Step 2: Move VPC definition to a new module
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
}
# Step 3: Use the removed block for the old VPC definition
removed {
from = aws_vpc.main
}
# Step 4: Run Terraform commands
terraform plan
terraform apply
Example 5: Removing Orphaned State Entries
# Step 1: Identify orphaned state entries
# List resources in the state
terraform state list
# Step 2: Add a removed block for the orphaned resource
removed {
from = aws_security_group.orphaned
}
# Step 3: Run Terraform commands
terraform plan
terraform apply
Best Practices for Using the removed
Block
- Backup the State File: Always back up your
terraform.tfstate
file before making changes.cp terraform.tfstate terraform.tfstate.bak
- Document Changes: Clearly document why a resource was removed to ensure future maintainability.
- Test in Non-Production Environments: Verify the behavior of the
removed
block in a staging environment before applying it to production. - Review Impact with
plan
: Always runterraform plan
to validate that no unintended changes will occur. - Remove the
removed
Block After Use: Once the resource is removed from the state, delete theremoved
block to avoid confusion in future updates.
- Atlassian Jira Demo - January 4, 2025
- Revolutionizing Business Management with Microsoft Business Central - January 4, 2025
- Key Benefits of Converting HTML to PDF File Format - January 2, 2025