Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Terraform Tutorials: Step-by-Step Tutorial for Using the removed Block

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:

  1. Legacy Resource Cleanup: Resources created with Terraform are now managed manually or by another tool.
  2. Split Configurations: Moving resources from one Terraform workspace/module to another without impacting the infrastructure.
  3. Orphaned State Entries: Removing unused state entries that no longer exist in real infrastructure.
  4. Migration to a New Tool: Switching from Terraform to another IaC tool while leaving resources intact.
  5. Third-Party Resource Management: Transitioning resource management to another team or tool.
  6. Failed Deletions: When a resource deletion fails but you no longer want Terraform to track it.
  7. Resource Lock-In Prevention: Detaching resources that must remain after Terraform’s scope changes.
  8. State File Size Optimization: Removing large numbers of unmanaged resources from the state file to improve performance.
  9. Custom Manual Adjustments: Detaching resources that are being modified outside Terraform’s scope.
  10. Testing and Development: Temporarily removing a resource for testing purposes without actually deleting it.

How to Use the removed Block

Step-by-Step Process

  1. Identify the Resource to Remove
    Use terraform state list to find the resource in the Terraform state that you want to stop managing. terraform state list
  2. Add the removed Block
    Add a removed block to your configuration for the resource(s) you want to remove. removed { from = aws_instance.example }
  3. Run terraform plan
    Terraform will show that the specified resource will be removed from the state. terraform plan
  4. Apply the Changes
    Execute terraform apply to remove the resource from the state file. terraform apply
  5. Clean Up the Configuration
    After removing the resource from the state, you can remove the removed 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

  1. Backup the State File: Always back up your terraform.tfstate file before making changes. cp terraform.tfstate terraform.tfstate.bak
  2. Document Changes: Clearly document why a resource was removed to ensure future maintainability.
  3. Test in Non-Production Environments: Verify the behavior of the removed block in a staging environment before applying it to production.
  4. Review Impact with plan: Always run terraform plan to validate that no unintended changes will occur.
  5. Remove the removed Block After Use: Once the resource is removed from the state, delete the removed block to avoid confusion in future updates.

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x