Bitzu

Terraform Associate 004 ยท State and backends

Resource drift

Detecting drift via plan and -refresh-only, applying vs reverting drift, out-of-band changes (6d).

Key points

  • Drift is when real infrastructure changes outside Terraform.
  • Every normal plan refreshes state first, so drift shows up there. terraform plan -refresh-only (and apply -refresh-only) only updates state to match reality and changes no infrastructure.
  • To revert drift, run a normal apply; to keep it, update the configuration to match.

Source: https://developer.hashicorp.com/terraform/cli/commands/plan

Practice questions

Single choice

What is the purpose of terraform apply -refresh-only?

  1. Update state to match real objects without changing infrastructure
  2. Force every resource back to what the configuration says
  3. Destroy resources that no longer match the configuration
  4. Download the latest provider plugins before planning
Show answer

Answer: A. Update state to match real objects without changing infrastructure

It records changes made outside Terraform into state and outputs, without modifying real infrastructure. It does not enforce the configuration or destroy anything.

Select all that apply

Which statements about handling drift are correct?

  1. A normal plan refreshes state first, so it can reveal drift
  2. -refresh-only updates state to match changes made outside Terraform
  3. A normal apply can revert drifted settings to the configured values
  4. -refresh-only also modifies the remote objects to match the configuration
  5. -refresh-only can be combined with -destroy in one run
Show answer

Answer: A. A normal plan refreshes state first, so it can reveal drift, B. -refresh-only updates state to match changes made outside Terraform, C. A normal apply can revert drifted settings to the configured values

Normal planning detects drift via refresh, -refresh-only records it without touching infrastructure, and a normal apply pushes real objects back to the configuration. -refresh-only never changes remote objects and cannot be combined with other modes.

True or false

True or false: terraform apply -refresh-only changes real cloud resources so that they match your configuration files.

Show answer

Answer: False

False. Refresh-only mode updates the state and root outputs to reflect reality; it leaves the infrastructure alone.

Practice 8 questions on this topic in Bitzu

Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module