Terraform

Which commands help you inspect, migrate, and safely manipulate Terraform state?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

terraform init -migrate-state
terraform plan -refresh-only
terraform state list
terraform show -json | jq '.values.root_module.resources[].address' | head
terraform state mv module.vpc.aws_subnet.public[0] module.vpc.aws_subnet.public_a
  • terraform init -migrate-state: moves an existing local state file into the configured backend with locking.

  • terraform plan -refresh-only: detects drift by refreshing remote object attributes without proposing changes.

  • terraform state list: confirms which objects are tracked in the state after migrations or imports.

  • terraform show -json: enables scripted inspections (paired here with jq) so you can audit addresses or metadata.

  • terraform state mv: renames or relocates resources/modules without recreation, useful during refactors.