Module 6: Terraform State Management
Lab 6B: Drift, Refresh-Only, moved, removed, and state rm
- Time
- 25 min
- Cost
- $0
- Prerequisites
- None listed
- Objectives
Starting file
Create ~/tf-course/lab6b/main.tf. This lab uses the local backend:
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"
}cd ~/tf-course/lab6b && terraform init && terraform apply -auto-approvePart 1: Drift
rm alpha.txtterraform plan -refresh-onlyExpected output: Terraform reports that local_file.alpha was deleted outside of Terraform, and that applying this refresh-only plan would update state to match.
terraform apply -refresh-only -auto-approve
terraform state list
terraform planExpected 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.
terraform apply -auto-approveExpected 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:
moved {
from = local_file.alpha
to = local_file.first
}Then run:
terraform planExpected 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:
terraform state mv local_file.first local_file.alpha
terraform planExpected 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:
removed {
from = local_file.beta
lifecycle {
destroy = false
}
}Then run:
terraform planExpected 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:
ls beta.txt # still exists
terraform state list # beta gonePart 4: The state rm trap
Leave the gamma block in the configuration and run:
terraform state rm local_file.gamma
terraform planExpected 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
terraform destroy -auto-approve
rm -f beta.txt # no longer managed, so Terraform won't delete itThis lab has no verification checklist in the course file, so it counts as complete when every step is checked.