Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 9Name in the course: Objective 6: Terraform state management

Manage state, backends, locking and drift

State is Terraform's memory. Alone you can keep it in a local file; in a team it must be remote, locked while someone runs, and protected. This chapter covers backends, locking, and what to do when reality drifts away from the code.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Local and remote state, locking and drift

9.1

Know the local backend

Without a backend block, state is saved in terraform.tfstate in the working folder (the local backend).

  • Good for learning and one-person tests.
  • Bad for teams: no sharing, no locking, easy to lose or to commit by mistake.

9.2

Use a remote backend

The backend block in the terraform block saves state remotely. On Azure: a storage account container.

backend.tf
terraform {
  backend "azurerm" {
    resource_group_name  = "rg-tfstate"
    storage_account_name = "sttfstate2026"
    container_name       = "tfstate"
    key                  = "shop/prod.tfstate"
    use_azuread_auth     = true
  }
}
az storage account create -g rg-tfstate -n sttfstate2026 --sku Standard_ZRS --min-tls-version TLS1_2 --allow-blob-public-access false
az storage container create --account-name sttfstate2026 -n tfstate --auth-mode login
terraform init -migrate-state
BackendLocking
azurerm (Azure Storage)Blob lease
s3 (AWS)S3 lock file (use_lockfile) or DynamoDB
gcs (Google)Built in
HCP Terraform (cloud block)Built in, plus runs and history

Remember

The backend block can not use variables. Use -backend-config=FILE for values that differ per environment (partial configuration).

9.3

Know state locking

While a plan or apply runs, the backend locks the state, so two runs can not change it at the same time.

terraform apply
Acquiring state lock. This may take a few moments...
terraform force-unlock LOCK_ID
terraform plan -lock-timeout=5m

Watch out

Use force-unlock only when you are sure no other run is active (for example after a crashed pipeline). Two parallel applies can corrupt the state.

9.4

Find and fix drift

Drift = someone changed a resource outside Terraform (portal, CLI). The next plan shows it.

SituationWhat to do
The manual change was wrongterraform apply puts the code's values back
The manual change is rightChange the code to match, then plan shows no changes
Only update the state with real valuesterraform apply -refresh-only
A resource was deleted by handPlan shows it will be created again
terraform plan -refresh-only
terraform apply -refresh-only
terraform plan -detailed-exitcode ; echo $?

-detailed-exitcode returns 2 when there are changes — a nightly drift check in CI can alert on that.

9.5

Read the state safely

Never edit the state file by hand. Use the state commands.

terraform state list
terraform state show azurerm_virtual_network.app
terraform state pull > backup.tfstate

More state commands (move, remove, import) are in Maintain infrastructure.

All commands in this chapter