{"id":47478,"date":"2024-11-26T16:12:58","date_gmt":"2024-11-26T16:12:58","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47478"},"modified":"2024-11-26T16:12:58","modified_gmt":"2024-11-26T16:12:58","slug":"terraform-tutorials-step-by-step-tutorial-for-using-the-removed-block","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-step-by-step-tutorial-for-using-the-removed-block\/","title":{"rendered":"Terraform Tutorials: Step-by-Step Tutorial for Using the removed Block"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>Step-by-Step Tutorial for Using the <code>removed<\/code> Block in Terraform<\/strong><\/h3>\n\n\n\n<p>The <code>removed<\/code> block in Terraform (introduced in version <strong>1.7<\/strong>) 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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the <code>removed<\/code> Block?<\/strong><\/h3>\n\n\n\n<p>The <code>removed<\/code> block explicitly informs Terraform that certain resources are no longer managed by the configuration. During <code>terraform apply<\/code>, Terraform removes these resources from the state without attempting to delete them in the actual infrastructure.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">removed {\n  <span class=\"hljs-keyword\">from<\/span> = aws_instance.example\n\n  lifecycle {\n    destroy = <span class=\"hljs-literal\">false<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>When to Use the <code>removed<\/code> Block<\/strong><\/h3>\n\n\n\n<p>Here are <strong>10 scenarios<\/strong> where the <code>removed<\/code> block is useful:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Legacy Resource Cleanup<\/strong>: Resources created with Terraform are now managed manually or by another tool.<\/li>\n\n\n\n<li><strong>Split Configurations<\/strong>: Moving resources from one Terraform workspace\/module to another without impacting the infrastructure.<\/li>\n\n\n\n<li><strong>Orphaned State Entries<\/strong>: Removing unused state entries that no longer exist in real infrastructure.<\/li>\n\n\n\n<li><strong>Migration to a New Tool<\/strong>: Switching from Terraform to another IaC tool while leaving resources intact.<\/li>\n\n\n\n<li><strong>Third-Party Resource Management<\/strong>: Transitioning resource management to another team or tool.<\/li>\n\n\n\n<li><strong>Failed Deletions<\/strong>: When a resource deletion fails but you no longer want Terraform to track it.<\/li>\n\n\n\n<li><strong>Resource Lock-In Prevention<\/strong>: Detaching resources that must remain after Terraform&#8217;s scope changes.<\/li>\n\n\n\n<li><strong>State File Size Optimization<\/strong>: Removing large numbers of unmanaged resources from the state file to improve performance.<\/li>\n\n\n\n<li><strong>Custom Manual Adjustments<\/strong>: Detaching resources that are being modified outside Terraform&#8217;s scope.<\/li>\n\n\n\n<li><strong>Testing and Development<\/strong>: Temporarily removing a resource for testing purposes without actually deleting it.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to Use the <code>removed<\/code> Block<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step-by-Step Process<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Identify the Resource to Remove<\/strong><br>Use <code>terraform state list<\/code> to find the resource in the Terraform state that you want to stop managing. <code>terraform state list<\/code><\/li>\n\n\n\n<li><strong>Add the <code>removed<\/code> Block<\/strong><br>Add a <code>removed<\/code> block to your configuration for the resource(s) you want to remove. <code>removed { from = aws_instance.example }<\/code><\/li>\n\n\n\n<li><strong>Run <code>terraform plan<\/code><\/strong><br>Terraform will show that the specified resource will be removed from the state. <code>terraform plan<\/code><\/li>\n\n\n\n<li><strong>Apply the Changes<\/strong><br>Execute <code>terraform apply<\/code> to remove the resource from the state file. <code>terraform apply<\/code><\/li>\n\n\n\n<li><strong>Clean Up the Configuration<\/strong><br>After removing the resource from the state, you can remove the <code>removed<\/code> block if it is no longer needed.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5 Code Examples of the <code>removed<\/code> Block<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 1: Removing a Single Resource<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Identify the resource<\/span>\nresource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  ami           = <span class=\"hljs-string\">\"ami-12345678\"<\/span>\n  instance_type = <span class=\"hljs-string\">\"t2.micro\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Step 2: Replace the resource block with the removed block<\/span>\nremoved {\n  from = aws_instance.example\n}\n\n<span class=\"hljs-comment\"># Step 3: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 2: Removing Multiple Resources<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Define multiple resources (before removal)<\/span>\nresource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"web\"<\/span> {\n  ami           = <span class=\"hljs-string\">\"ami-12345678\"<\/span>\n  instance_type = <span class=\"hljs-string\">\"t2.micro\"<\/span>\n}\n\nresource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"db\"<\/span> {\n  ami           = <span class=\"hljs-string\">\"ami-87654321\"<\/span>\n  instance_type = <span class=\"hljs-string\">\"t3.micro\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Step 2: Add removed blocks for both resources<\/span>\nremoved {\n  from = aws_instance.web\n}\n\nremoved {\n  from = aws_instance.db\n}\n\n<span class=\"hljs-comment\"># Step 3: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 3: Removing Dynamically Created Resources<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Original resource definition<\/span>\nresource <span class=\"hljs-string\">\"aws_s3_bucket\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  for_each = toset(&#91;<span class=\"hljs-string\">\"bucket1\"<\/span>, <span class=\"hljs-string\">\"bucket2\"<\/span>, <span class=\"hljs-string\">\"bucket3\"<\/span>])\n  bucket   = each.key\n}\n\n<span class=\"hljs-comment\"># Step 2: Replace the resource block with removed blocks<\/span>\nremoved {\n  from = aws_s3_bucket.example&#91;<span class=\"hljs-string\">\"bucket1\"<\/span>]\n}\n\nremoved {\n  from = aws_s3_bucket.example&#91;<span class=\"hljs-string\">\"bucket2\"<\/span>]\n}\n\nremoved {\n  from = aws_s3_bucket.example&#91;<span class=\"hljs-string\">\"bucket3\"<\/span>]\n}\n\n<span class=\"hljs-comment\"># Step 3: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 4: Transitioning to a New Module<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Original resource definition<\/span>\nresource <span class=\"hljs-string\">\"aws_vpc\"<\/span> <span class=\"hljs-string\">\"main\"<\/span> {\n  cidr_block = <span class=\"hljs-string\">\"10.0.0.0\/16\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Step 2: Move VPC definition to a new module<\/span>\nmodule <span class=\"hljs-string\">\"network\"<\/span> {\n  source     = <span class=\"hljs-string\">\".\/modules\/network\"<\/span>\n  cidr_block = <span class=\"hljs-string\">\"10.0.0.0\/16\"<\/span>\n}\n\n<span class=\"hljs-comment\"># Step 3: Use the removed block for the old VPC definition<\/span>\nremoved {\n  from = aws_vpc.main\n}\n\n<span class=\"hljs-comment\"># Step 4: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 5: Removing Orphaned State Entries<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Step 1: Identify orphaned state entries<\/span>\n<span class=\"hljs-comment\"># List resources in the state<\/span>\nterraform state <span class=\"hljs-keyword\">list<\/span>\n\n<span class=\"hljs-comment\"># Step 2: Add a removed block for the orphaned resource<\/span>\nremoved {\n  from = aws_security_group.orphaned\n}\n\n<span class=\"hljs-comment\"># Step 3: Run Terraform commands<\/span>\nterraform plan\nterraform apply\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Best Practices for Using the <code>removed<\/code> Block<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Backup the State File<\/strong>: Always back up your <code>terraform.tfstate<\/code> file before making changes. <code>cp terraform.tfstate terraform.tfstate.bak<\/code><\/li>\n\n\n\n<li><strong>Document Changes<\/strong>: Clearly document why a resource was removed to ensure future maintainability.<\/li>\n\n\n\n<li><strong>Test in Non-Production Environments<\/strong>: Verify the behavior of the <code>removed<\/code> block in a staging environment before applying it to production.<\/li>\n\n\n\n<li><strong>Review Impact with <code>plan<\/code><\/strong>: Always run <code>terraform plan<\/code> to validate that no unintended changes will occur.<\/li>\n\n\n\n<li><strong>Remove the <code>removed<\/code> Block After Use<\/strong>: Once the resource is removed from the state, delete the <code>removed<\/code> block to avoid confusion in future updates.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-47478","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47478"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47478\/revisions"}],"predecessor-version":[{"id":47479,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47478\/revisions\/47479"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}