Module 6: Terraform State Management

Lab 6B: Drift, Refresh-Only, moved, removed, and state rm

Time
25 min
Cost
$0
Prerequisites
None listed
Objectives
0 of 8 checked

Starting file

Create ~/tf-course/lab6b/main.tf. This lab uses the local backend:

~/tf-course/lab6b/main.tfHCL
terraform {
  required_version = "~> 1.12.0"

  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

resource "local_file" "alpha" {
  filename = "${path.module}/alpha.txt"
  content  = "alpha\n"
}

resource "local_file" "beta" {
  filename = "${path.module}/beta.txt"
  content  = "beta\n"
}

resource "local_file" "gamma" {
  filename = "${path.module}/gamma.txt"
  content  = "gamma\n"
}
Shell
cd ~/tf-course/lab6b && terraform init && terraform apply -auto-approve

Part 1: Drift

Shell
rm alpha.txt

Shell
terraform plan -refresh-only

Expected output: Terraform reports that local_file.alpha was deleted outside of Terraform, and that applying this refresh-only plan would update state to match.

Shell
terraform apply -refresh-only -auto-approve
terraform state list
terraform plan

Expected output: alpha disappears from state list. But the configuration still declares it, so the normal plan shows + local_file.alpha (create). Recording the drift didn't make the code agree with reality.

Shell
terraform apply -auto-approve

Expected output: alpha.txt is recreated.

Part 2: Rename with moved

Rename the resource local_file.alpha to local_file.first. Keep the same filename and content. Add this block:

HCL
moved {
  from = local_file.alpha
  to   = local_file.first
}

Then run:

Shell
terraform plan

Expected output: # local_file.alpha has moved to local_file.first, and Plan: 0 to add, 0 to change, 0 to destroy. Apply it.

Delete the moved block. Rename local_file.first back to local_file.alpha in the configuration, then run:

Shell
terraform state mv local_file.first local_file.alpha
terraform plan

Expected output: Move "local_file.first" to "local_file.alpha", then No changes. It works too, but it changed state immediately, with no plan and nothing for a reviewer to see.

Part 3: Stop managing with removed

Delete the resource "local_file" "beta" block and add:

HCL
removed {
  from = local_file.beta

  lifecycle {
    destroy = false
  }
}

Then run:

Shell
terraform plan

Expected output: the plan shows that local_file.beta will be removed from state but not destroyed, with a warning saying the object will no longer be managed. Apply it, then check:

Shell
ls beta.txt              # still exists
terraform state list     # beta gone

Part 4: The state rm trap

Leave the gamma block in the configuration and run:

Shell
terraform state rm local_file.gamma
terraform plan

Expected output: Removed local_file.gamma, then the plan proposes creating local_file.gamma again. Because the block still exists, Terraform believes the resource is missing. For local_file, the create overwrites the same path. For a cloud resource, it would create a duplicate or fail with a name conflict.

Apply the plan to bring it back under management.

Cleanup

Shell
terraform destroy -auto-approve
rm -f beta.txt    # no longer managed, so Terraform won't delete it

Lab 6B complete. Every step is checked.

This lab has no verification checklist in the course file, so it counts as complete when every step is checked.