Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 9.1Terraform, Chapter 9

Work with state: inspect, move, remove, import and find drift

Level
Medium
45 min
Machines
any computer with Terraform 1.6 or newer (no cloud account needed)
Status
Not started

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 (import and removed blocks need 1.5 / 1.7; terraform state rm works everywhere).
  • Make this main.tf and run terraform init && terraform apply -auto-approve:
    main.tf
    resource "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

  1. List all resources in the state and show the details of local_file.web.
  2. Rename local_file.web to local_file.frontend in the code, and use a moved block so the plan shows no destroy.
  3. Remove local_file.db from Terraform management without deleting the file db.conf.
  4. Make a file cache.conf by hand (port=6379). Write a resource for it and bring it into the state with an import block. The plan must show import, not create.
  5. Make drift: change web.conf by hand to port=8080. Find the drift with a refresh-only plan, then fix it with a normal apply.
  6. Make a backup of the state and explain where the lock file and the backup are.

Check your work

terraform state list
terraform plan
ls
cat web.conf
  • terraform state list shows local_file.cache and local_file.frontend, and not local_file.db or local_file.web.
  • terraform plan says No changes.
  • db.conf still exists on disk.
  • web.conf has port=80 again.

Hints

Show the hints

Solution

Show the solution
  1. Inspect the state.
    terraform state list
    local_file.db
    local_file.web
    terraform state show local_file.web
  2. Rename with a moved block (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
    }
    terraform plan
    terraform apply -auto-approve

    The plan says local_file.web has moved to local_file.frontend and 0 to destroy. The CLI way is terraform state mv local_file.web local_file.frontend.

  3. 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.db block from main.tf; ls still shows db.conf.

  4. Import an existing file.
    printf 'port=6379\n' > cache.conf
    main.tf (part)
    resource "local_file" "cache" {
      filename = "${path.module}/cache.conf"
      content  = "port=6379\n"
    }
     
    import {
      to = local_file.cache
      id = "./cache.conf"
    }
    terraform plan
    terraform apply -auto-approve
  5. Find and fix drift.
    printf 'port=8080\n' > web.conf
    terraform plan -refresh-only
    terraform apply -auto-approve
    cat web.conf
    port=80
  6. Back up the state.
    cp terraform.tfstate state-backup-$(date +%F).tfstate
    ls -a

    terraform.tfstate.backup is the state before the last change. .terraform.tfstate.lock.info exists 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.

Lessons for this lab

Commands in this lab