BM: Baca dalam Bahasa Melayu
World T3: State & Teams
moved blocks and refactors
You will learn: how to rename things in the blueprint without tearing them down.
Checked with OpenTofu 1.13 on 1 Nov 2026
The problem
You renamed random_pet.stall to random_pet.chef in main.tf. Nothing else. Read the plan.
Tap what OpenTofu will do to each one.
+ create~ update in place- destroy-/+ replace
0 of 2 read
# random_pet.chef + resource "random_pet" "chef" { + id = (known after apply) + length = 2 + separator = "-" }
What happens to random_pet.chef?
# random_pet.stall - resource "random_pet" "stall" { - id = "lucky-civet" -> null - length = 2 -> null - separator = "-" -> null }
What happens to random_pet.stall?
The summary line shows when you finish.
Watch
Tap the line with the old name.
0 of 1 found
OpenTofu knows things by their address. A new name looks like a new thing. A moved block: Code that says: same thing, new address. A rename then changes nothing real. says: “Same thing, new name.”
Try it
Add the moved block to main.tf. Then tofu plan: look for has moved to. Then tofu apply.
Blueprint (your .tf files)init done
local_file.menurandom_pet.chefnot built yet
Built (the state file)state #1
local_file.menurandom_pet.stall
- stall
- lucky-civet
Change a file, then run tofu plan to see what it means.
Hint
Copy the four lines from the last card.Predict
Next refactor: count becomes for_each. So random_pet.stall[0] becomes random_pet.stall["nasi"]. What keeps it alive?
Pick one answer.
Check
Which changes need a moved block?
Drag a card into a box. Or tap the card, then tap the box.
Watch
The old way edits the register directly. Nobody reviews it. Build it anyway, so you can read it.
Tap the pieces in the right order.
Check
You review a teammate’s refactor. Swipe: good, or needs work?
Swipe right for good, left for needs work. Or use the buttons.
Renamed a resource and added a moved block in the same change
You did it!
You can now rename and reshuffle the blueprint without rebuilding anything.
Go deeper (optional)
moved also renames whole modules: from = module.a, to = module.b. To stop managing a thing without destroying it, use a removed block with lifecycle { destroy = false }. A module others use should keep its moved blocks for a long time.
Real mission
tofu init && tofu applytofu plantofu plantofu apply
0 of 4 steps done · Your ticks are saved.
Bahasa Melayu
Anda tukar nama random_pet.stall kepada random_pet.chef dalam main.tf. Itu sahaja. Baca plan ini.
OpenTofu kenal benda melalui alamatnya. Nama baharu nampak macam benda baharu. Blok moved (blok pindah) berkata: “Benda sama, nama baharu.”
Tambah blok moved dalam main.tf. Kemudian tofu plan: cari has moved to. Kemudian tofu apply.
Refactor seterusnya: count jadi for_each. Jadi random_pet.stall[0] jadi random_pet.stall["nasi"]. Apa yang selamatkannya?
Perubahan mana yang perlukan blok moved?
Cara lama ubah daftar terus. Tiada siapa menyemaknya. Bina juga, supaya anda boleh membacanya.
Anda semak refactor rakan. Leret: bagus, atau perlu dibaiki?
Kini anda boleh tukar nama dan susun semula pelan tanpa bina semula apa-apa.
You finished this before. Practice is always welcome!
Official docs: OpenTofu: refactoring with moved blocks · tofu state mv · Terraform: moved block
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?