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
You edit the backend block to point at a different bucket. What must you do before plan will work?
- Run terraform init again
- Run terraform validate
- Run terraform refresh
- 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.
Which statements about the terraform_remote_state data source are correct?
- It exposes the root module outputs of another configuration's state
- Reading it needs access to the whole state snapshot
- Outputs of nested modules must be re-exported from the root module to be visible
- It exposes every resource attribute stored in the remote state
- 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: 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.
