Goal
State is where most real Terraform trouble starts. In this lab you look inside the state, rename a resource without destroying it (moved block and terraform state mv), stop managing a resource without deleting it, import an existing file, and find and fix drift. Everything uses the local provider, so you can break things safely.
Before you start
- Make the folder
~/tf-lab2. Terraform 1.6 or newer (importandremovedblocks need 1.5 / 1.7;terraform state rmworks everywhere). - Make this
main.tfand runterraform init && terraform apply -auto-approve:main.tfresource "local_file" "web" { filename = "${path.module}/web.conf" content = "port=80\n" } resource "local_file" "db" { filename = "${path.module}/db.conf" content = "port=5432\n" }
Tasks
Tick a task when it is done. Only your browser remembers it.
- List all resources in the state and show the details of
local_file.web. - Rename
local_file.webtolocal_file.frontendin the code, and use amovedblock so the plan shows no destroy. - Remove
local_file.dbfrom Terraform management without deleting the filedb.conf. - Make a file
cache.confby hand (port=6379). Write a resource for it and bring it into the state with animportblock. The plan must show import, not create. - Make drift: change
web.confby hand toport=8080. Find the drift with a refresh-only plan, then fix it with a normal apply. - Make a backup of the state and explain where the lock file and the backup are.
Check your work
terraform state listshowslocal_file.cacheandlocal_file.frontend, and notlocal_file.dborlocal_file.web.terraform plansays No changes.db.confstill exists on disk.web.confhasport=80again.
Hints
Show the hints
terraform state list,terraform state show ADDRESS.moved { from = local_file.web to = local_file.frontend }.terraform state rm local_file.db(or aremovedblock withlifecycle { destroy = false }in Terraform 1.7+), then delete its block from the code.import { to = local_file.cache id = "./cache.conf" }. Theidof alocal_fileis its path. The content in the code must match the file, or the plan replaces it.terraform plan -refresh-onlyshows what changed outside Terraform.
Solution
Show the solution
- Inspect the state.
- Rename with a
movedblock (change the resource name too).main.tf (part)resource "local_file" "frontend" { filename = "${path.module}/web.conf" content = "port=80\n" } moved { from = local_file.web to = local_file.frontend }The plan says
local_file.web has moved to local_file.frontendand0 to destroy. The CLI way isterraform state mv local_file.web local_file.frontend. - Forget the database file but keep it.
terraform state rm local_file.db Removed local_file.db Successfully removed 1 resource instance(s).Then delete the
local_file.dbblock frommain.tf;lsstill showsdb.conf. - Import an existing file.
printf 'port=6379\n' > cache.confmain.tf (part)resource "local_file" "cache" { filename = "${path.module}/cache.conf" content = "port=6379\n" } import { to = local_file.cache id = "./cache.conf" } - Find and fix drift.
- Back up the state.
terraform.tfstate.backupis the state before the last change..terraform.tfstate.lock.infoexists only while a command runs (the local lock). With a remote backend (Azure Storage, S3 + DynamoDB, HCP Terraform) the lock and the versions live in the backend.
My notes
No notes yet.