Bitzu

Terraform Associate 004 ยท State and backends

Changing backends and migrating state

Re-running init after changing a backend, -migrate-state vs -reconfigure, terraform_remote_state data source (6c).

Key points

  • After you change or remove a backend block, run terraform init again.
  • Supply -migrate-state to copy existing state to the new backend, or -reconfigure to adopt the new settings and skip any migration.
  • To read another configuration's root outputs, use the terraform_remote_state data source.

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

Practice questions

Single choice

You edit the backend block to point at a different bucket. What must you do before plan will work?

  1. Run terraform init again
  2. Run terraform validate
  3. Run terraform refresh
  4. Delete the .terraform.lock.hcl file
Show answer

Answer: A. Run terraform init again

Terraform must re-run init after any backend change so it can reconfigure and optionally migrate state. Plan and apply do not detect and apply the change on their own.

Select all that apply

Which statements about the terraform_remote_state data source are correct?

  1. It exposes the root module outputs of another configuration's state
  2. Reading it needs access to the whole state snapshot
  3. Outputs of nested modules must be re-exported from the root module to be visible
  4. It exposes every resource attribute stored in the remote state
  5. It requires installing a separate third-party provider
Show answer

Answer: A. It exposes the root module outputs of another configuration's state, B. Reading it needs access to the whole state snapshot, C. Outputs of nested modules must be re-exported from the root module to be visible

It exposes root module outputs of another state through an outputs attribute and needs access to the full state snapshot. It cannot reach resource attributes or nested module outputs directly.

True or false

True or false: terraform init -reconfigure copies your existing state into the newly configured backend.

Show answer

Answer: False

False. -reconfigure ignores the existing backend configuration and prevents state migration; -migrate-state is the flag that copies state.

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