Terraform & IaC (50 Qs)•Part 2: Modules, Multi-Environment & GitOps (16 - 30)
How do you refactor Terraform code without destroying existing cloud resources using the `moved` block?
mid LevelCode ExampleFormsCI/CD & DevOpsTerraform & IaC
In Simple Words: In older Terraform, renaming a resource in HCL caused Terraform to destroy the old resource and create a new one.
Starting in Terraform 1.1, the moved block instructs Terraform to update the state file mapping without touching the physical cloud infrastructure:
hcl
# Refactoring: Moving a resource into a module without recreation
moved {
from = aws_instance.api
to = module.backend.aws_instance.api
}