#moved-block-vs-state-mv
What is the `moved {}` block for, and why is it better than `terraform state mv`?
Что отвечать
`moved {}` declares a resource move in HCL: "what used to be `module.old.x` is now `module.new.x`." At plan, Terraform sees the block and moves the state entry as part of the change set, without destroy and create. The key difference from `state mv` is that it is declarative. The block is committed to the repo, reviewed in a PR, and visible in a colleague's `terraform plan`. Once everyone has run `apply`, you can drop the block a few releases later. `state mv` mutates state locally, shows up nowhere in the PR, and forces colleagues to do the same thing by hand, which is a source of drift between people.
Что хотят услышать
A senior should: - note that the moved block arrived in 1.1 as the standard refactoring mechanism - explain the grace period: the block stays in HCL for a few releases until every environment applies it, then it is removed - say that moved works only for addresses: rename a resource, move it into a child module, change a prefix. It does not work for changing the resource type or the provider - mention that one block describes one resource move; a mass refactor takes several blocks, and the review needs care
Подводные камни
- ✗ Using `state mv` in a team. Your state knows, your colleagues' state does not, and on their apply they see destroy and create
- ✗ Removing the moved block before every environment has applied it. The environments that have not applied yet get destroy and create
- ✗ Thinking moved can change a resource type (`aws_s3_bucket` to `aws_s3_bucket_v2`). It cannot; a type change needs import plus rm
Follow-up
- ? How many releases should you keep a moved block in the HCL?
- ? How do you rename a resource across providers (`aws` to `awscc`)?
- ? What happens if two moved blocks contradict each other?
Глубина в базе знаний