CertLumen

HashiCorp Terraform Associate (004) · Section 6 · 10% of the exam

Manage resource drift and Terraform state

One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Terraform state management”. Here is what it covers, the points to remember and a question to try.

What it covers

Drift is when real infrastructure no longer matches what Terraform recorded. This lesson shows how Terraform detects drift, how to accept or revert it with refresh-only runs and apply, and how to inspect and change state safely with the state commands, moved, removed and import blocks, and the replace option.

Points to remember

  • Drift is a change to a managed object made outside Terraform.
  • Every plan and apply refreshes state in memory and reports 'Objects have changed outside of Terraform'.
  • terraform plan -refresh-only shows drift without changing state or infrastructure.
  • terraform apply -refresh-only writes real values to state but does not change infrastructure.
  • A normal terraform apply reverts drift on arguments that the configuration sets.

Try a question

A VM already exists, created by hand. The team uses Terraform 1.12 and wants to adopt it through the plan-and-apply workflow, with Terraform drafting the resource block. Which approach fits?

  1. AAdd an import block, then run terraform plan -generate-config-out=generated.tf
  2. BRun terraform state push with a hand-written JSON snippet describing the VM
  3. CRun terraform apply -refresh-only and the VM is discovered into state automatically
  4. DAdd lifecycle { create_before_destroy = true } to a new resource block for the VM
Show the answer

A. Terraform 1.5 introduced the import block. The to argument names the resource address and id is the provider-specific identifier. -generate-config-out writes draft configuration for resources that have no block yet; review it before applying.

Written for practice in the style of the exam: never a question from the real exam.

In the course

The lesson Resource drift and managing Terraform state teaches this objective (about 40 minutes), and 19 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.

More in this section

← Previous objective · All 37 objectives · Next objective →