Migrating State Between Backends
Every project starts with local state and should not stay there. Later you may move state between buckets or accounts, or switch between Terraform and OpenTofu. The CLI copies state for you whenever the backend configuration changes: you change the block, run init -migrate-state, and check the plan is empty.
The procedure
- Announce a freeze: nobody applies this configuration until you're done (locking protects writes, not your migration).
- Back up the current state:
tofu state pull > backup.tfstate. - Change the
backendblock (or the-backend-configfile). tofu init -migrate-state: it detects the change, asks whether to copy the state (-force-copyanswers yes), and writes it to the new backend.tofu planmust say No changes. If it wants to create everything, the state didn't arrive where the configuration looks for it.- Remove or archive the old state so nobody uses it by mistake.
Local to another location, for real
The local backend with a path stands in for S3 here; the commands and messages are the same for every backend type:
mkdir app && cd app
cat > main.tf <<'HCL'
resource "terraform_data" "release" {
input = "1.4.0"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
tofu state pull > ../backup.tfstate
mkdir ../shared-state
cat > backend.tf <<'HCL'
terraform {
backend "local" {
path = "../shared-state/app.tfstate"
}
}
HCL
echo "--- plan with a changed backend, before init"
tofu plan 2>&1 | grep -E '^Error|^Reason'
echo "--- migrate"
tofu init -migrate-state -force-copy | grep -E 'backend|Success'
echo "--- where the state is now"
jq -r '.resources[].type' ../shared-state/app.tfstate
tofu plan | tail -1--- plan with a changed backend, before init
Error: Backend initialization required, please run "tofu init"
Reason: Initial configuration of the requested backend "local"
--- migrate
Initializing the backend...
Successfully configured the backend "local"! OpenTofu will automatically
use this backend unless the backend configuration changes.
If you ever set or change modules or backend configuration for OpenTofu,
--- where the state is now
terraform_data
No changes. Your infrastructure matches the configuration.
The CLI refuses to work until you re-initialise, so a backend change can't silently start a fresh, empty state. Two flags decide what init does with the old state:
| Flag | Meaning |
|---|---|
-migrate-state |
copy the existing state to the new backend (ask first; -force-copy = yes) |
-reconfigure |
use the new backend and ignore the old state: correct only when the new backend already holds the right state |
Local to S3
A sample session for the most common migration, after adding the backend "s3" block from Remote Backends:
$ tofu init -migrate-state
Initializing the backend...
Do you want to copy existing state to the new backend?
Pre-existing state was found while migrating the previous "local" backend to the
newly configured "s3" backend. No existing state was found in the newly
configured "s3" backend. Do you want to copy this state to the new "s3"
backend? Enter "yes" to copy and "no" to start with an empty state.
Enter a value: yes
Successfully configured the backend "s3"! OpenTofu will automatically
use this backend unless the backend configuration changes.
…
$ tofu plan
Acquiring state lock. This may take a few moments...
…
No changes. Your infrastructure matches the configuration.
$ rm terraform.tfstate terraform.tfstate.backup # the copy in S3 is now the truthMoving between two S3 buckets is the same: change bucket/key in the backend config and run init -migrate-state. With workspaces, all of them are migrated together.
Splitting one state into two
Moving some resources to another configuration (the network leaves the monolith) is a different job: no backend changes. Do it declaratively:
- In the old configuration, delete the blocks and add a
removedblock for each address (from = aws_vpc.main,lifecycle { destroy = false }). Apply: the objects are forgotten, not destroyed. - In the new configuration, add the blocks and an
importblock for each (to = aws_vpc.main,id = "vpc-0a1b…"). Plan (only imports), apply.
Each step is reviewed and every state stays consistent. The older way, tofu state mv -state-out=…, only works with local state files and skips review.
Switching CLIs
The state format is shared, so switching between the CLIs needs no conversion as long as the configuration avoids features only one of them has. From Terraform 1.5.x and earlier, OpenTofu is a drop-in replacement; from later Terraform versions, OpenTofu publishes a migration guide per version listing what to change first (for example, removing Terraform-only blocks). Switching is: install the other CLI, run init (it rewrites the lock file with its registry's addresses), plan, and check the plan is empty. If you enabled OpenTofu's state encryption, decrypt first: Terraform cannot read encrypted state. The OpenTofu migration guides list version-specific steps.
Test yourself
You changed the backend block. What must you run?
tofu apply -migratetofu init -migrate-statetofu state push- Nothing, plan migrates automatically
After a migration, tofu plan wants to create every resource. What most likely happened?
- The migration worked
- The state was not copied to where the new backend looks (e.g.
-reconfigurewas used or the key is wrong) - The providers changed
- Locking failed
How do you move a VPC from one configuration's state to another's in a reviewable way?
- Copy the state file
removed(destroy = false) in the old configuration,importin the new one-reconfiguretofu taint