Scenario
You are operating a production Terraform estate. The team
upgraded the terraform-aws-modules/vpc/aws module from 5.0.0
to 5.1.0. The next plan is scheduled for the maintenance
window. You run terraform plan and see:
# module.network.aws_vpc.main will be destroyed
- resource "aws_vpc" "main" {
- cidr_block = "10.0.0.0/16" -> null
}
# module.network.aws_vpc.this will be created
+ resource "aws_vpc" "this" {
+ cidr_block = "10.0.0.0/16"
}
Plan: 1 to add, 0 to change, 1 to destroy.
The plan proposes to destroy the VPC and recreate it. The configuration has not changed.
Your task
Investigate the cause and recover without destroying the real VPC.
Evidence to discover
# Check the module release notes
# (the modules CHANGELOG is on GitHub)
# Check the modules source
grep -A5 "source" versions.tf
# Check the previous state
terraform state list
Questions to answer
- What changed in the module?
- Is the change intentional?
- What is the correct remediation?
- What is the verification step?
Recovery procedure
(Do not reveal this until the student has reasoned through the problem.)
- Read the module release notes. The 5.1.0 release renamed
the resource inside the module from
aws_vpc.maintoaws_vpc.this. - Identify the cause. The module upgraded the resource address. The state has the old address.
- Add a
movedblock.
# main.tf
moved {
from = module.network.aws_vpc.main
to = module.network.aws_vpc.this
}
- Re-plan.
terraform plan
The plan should be empty.
- Verify the state.
terraform state list
The state should have module.network.aws_vpc.this.
- Document the incident. The module upgrade, the changed
address, the
movedblock.
Remediation
- The cause was the modules renamed resource.
- The
movedblock preserves the state. - The plan is empty after the
movedblock. - The next apply is a no-op.
Prevention
- Use
movedblocks when refactoring modules. - Test module upgrades in a development environment.
- Read the module release notes for breaking changes.
- Pin module versions deliberately.
- Document the upgrade in the runbook.
What you learned
- A module upgrade may break the resource addresses.
- A
movedblock tells Terraform that the resource has been moved. - The state is updated to reflect the new address.
- The plan is empty after the
movedblock. - The
movedblock is the under-used feature that allows refactoring without recreating resources.