Skip to main content

Posts

Showing posts with the label Terraform

Refactoring Terraform Modules: Using "moved" Blocks to Avoid Destroy/Create

  Every Senior DevOps engineer has experienced the specific panic induced by a Terraform plan output showing   1 to destroy, 1 to add   on a stateful resource. You simply wanted to rename a resource variable for consistency or move a spaghetti-code   main.tf   resource into a reusable child module. However, Terraform interprets this change not as a migration, but as the deletion of the old resource and the provisioning of a new one. For stateless EC2 instances in an Auto Scaling Group, this is an inconvenience. For an RDS instance, an S3 bucket, or a production Load Balancer with hardcoded DNS pointers, this is catastrophic. Historically, the solution was the imperative  terraform state mv  command. This approach is brittle, manual, effectively invisible to code review, and breaks CI/CD automation. Since Terraform 1.1, the declarative  moved  block is the standard for solving this problem safely. The Root Cause: State Identity vs. Configurati...