Jan 18 2024 11:08 PM - edited Jan 19 2024 12:33 AM
Let's say you have an Azure environment managed by Terraform, and it includes several Azure Virtual Machines (VMs). Over time, you decide to decommission a VM, but you don't want to destroy it immediately. Instead, you prefer to remove it from Terraform's state management while keeping the VM intact in Azure for archival or auditing purposes.
Previously, you would have to use terraform state rm to manually remove the VM from the state file, a process that can be error-prone and lacks the visibility of Terraform's planning phase.
With the new removed block, you can now handle this scenario more elegantly and safely. Here's how you might write the Terraform configuration for this:
resource "azurerm_virtual_machine" "example_vm" {
# ... (VM configuration details) ...
}
# New 'removed' block usage
removed {
from = azurerm_virtual_machine.example_vm
lifecycle {
destroy = false
}
}
This example demonstrates how the removed block can be used to manage Azure resources more effectively. It offers a safer and more predictable way to handle resource lifecycles, especially for resources that are being decommissioned but not immediately destroyed.