Backend migration rehearsal
Plan a backend migration as a state operation
A backend migration changes where state is stored. It is not a resource move between regions, cloud accounts or providers. Before migration, record the source and destination backend types, endpoints, state keys, every workspace, permissions, encryption and locking behavior. Check for existing destination state and other owners; never assume a new configuration means an empty destination. Rehearse on approved isolated copies, freeze all writers, preserve both sides, and prepare a recovery owner and abort criteria.
Freeze writers -> inventory source and destination -> protect backups -> rehearse -> approve migration -> verify mappings and fresh plan -> reopen writes.The migration itself may access a backend, load providers or fetch modules during initialization; the following examples are command references only.
Choose migration or reconfiguration deliberately
When a configured backend changes, Terraform init can attempt to transfer the previous state using the migrate-state option. Reconfigure instead ignores the cached backend configuration and does not copy state. This is appropriate only when the operator intends to use the destination state as-is (for example a verified state that was migrated separately). It is not a repair for missing state.
terraform init -migrate-stateterraform init -reconfigureThese are alternative operations, not consecutive steps. Migration can prompt about workspace states. Do not suppress these confirmations with force-copy as a routine shortcut. A reconfigured empty destination may lead to a plan proposing to recreate existing infrastructure. Stop on unexpected plans. See https://developer.hashicorp.com/terraform/cli/commands/init.
Rehearse the destination layout
The local backend provides an illustrative pair of configuration stages, not executable migration instructions. A real migration needs a pre-existing, independently verified destination and controlled engine environment.
terraform { backend "local" { path = "source.tfstate" }}terraform { backend "local" { path = "destination.tfstate" }}The snippets are mutually exclusive versions of the same root configuration. They show the default-workspace path only; non-default workspaces require separately verified workspace directory mappings. The supplied offline fixture models the inventory and does not contain real state or perform a transfer. For a remote destination, check the exact backend documentation, credentials boundary, lock mechanism and version history. See https://developer.hashicorp.com/terraform/language/backend/s3 and https://developer.hashicorp.com/terraform/language/backend/gcs.
Distinguish mapping repair from backend migration
State mv changes which address is associated with an object; state rm removes the association without deleting the object itself. If the corresponding configuration remains after removal, a later plan can propose a new object. Prefer reviewed declarative moved and removed blocks when supported and appropriate. Import adopts an existing object under an intended address using provider-specific identity rules; verify that no other state manages it. State replace-provider updates the provider source association for matching resources, not their cloud location. It does not prove schema compatibility with a replacement provider. Coordinate configuration and dependency changes, verify the replacement source and plan.
Command reference — emergency or controlled change onlyterraform state mv <old-address> <new-address>terraform state rm <address>terraform state replace-provider <old-source> <new-source>terraform force-unlock <lock-id>Do not run these commands to make an unexpected migration plan disappear. Their effects and prerequisites differ. See https://developer.hashicorp.com/terraform/cli/commands/state.
Verify migration before reopening writes
Compare source and destination workspace inventories, address-to-object mappings, provider associations, output contracts and snapshot history. A matching serial alone is not sufficient. Run a fresh reviewed plan only in the authorized environment after checking actual identity and provider access. A plan with unexpected changes is a stop signal, not authorization to apply. Record the destination as authoritative, prevent old CI jobs from writing to the source, retain protected source backups under policy, and update consumers. Do not silently run both backends as active owners.
Fictional migration reviewsource workspaces: dev, testdestination workspaces: devmissing: testDecision: HOLD even if dev plan is emptyFor rollback, assess whether the destination has accepted newer writes; copying an old source snapshot back could lose newer mappings. Escalate if either side is uncertain and reconcile both against observed resources before approving a recovery.
Practice disaster recovery separately
Recovery point objective (RPO) describes the tolerable amount of lost history; recovery time objective (RTO) describes the target time to restore a capability. Set and test these separately for state management and application data. A state restore drill should measure access recovery, snapshot integrity, reconciliation and the ability to produce a safe plan; a service restore drill should verify restored data, connectivity and application behavior. The offline case checker verifies a limited fictional decision model. An eligible result means ready for human review, never permission to write or evidence of a working Terraform backend. Next: multi-cloud and hybrid architecture.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Backend migration rehearsal — knowledge check
Review state recovery, backend migration and disaster recovery.
Which init option attempts to copy existing backend state?
Two source workspaces exist but the destination has only one. What is the migration gate?
What does state rm do to the remote object directly?
What does state replace-provider change?
A destination accepts newer writes after migration. Why not restore the old source immediately?