Skip to main content
When you initialize Terraform with a remote backend, Terraform stores and updates the state file in that backend automatically. But if you already have an existing local state file (for example, you developed locally and now want to move to a remote backend), you can migrate the current local state into a remote backend without losing your existing state. This guide shows how to migrate a local Terraform state to an S3 backend and how to move it back to local when needed. It preserves the original state and walks through validation and verification steps.

Prerequisites

  • Terraform installed (1.x or later recommended).
  • AWS credentials configured for the account that owns the target S3 bucket.
  • An S3 bucket created to hold Terraform state and appropriate permissions (PutObject, GetObject, ListBucket).
  • Working Terraform configuration with a local state (no active terraform { backend ... } block).

Example: Resources Managed Locally

Here is a simple example of Terraform resources that might currently be tracked by a local state:

Step 1 — Add (or enable) a backend configuration

If your working directory does not currently have an active backend block (it may be commented out), add or uncomment the backend configuration that points to your S3 bucket. Example (commented version shown first for context):
Uncomment or create the active backend block:

Step 2 — Initialize and migrate the state

Run terraform init with the -migrate-state flag to initialize the backend and instruct Terraform to migrate any existing local state to the newly configured backend.
Terraform will detect the existing local state and prompt you to confirm copying that state to the S3 backend:
If you want to continue managing the same resources with Terraform, answer yes to copy your existing local state to the S3 backend. Choose no only if you intentionally want to start with an empty remote state.
After you confirm yes, Terraform configures the backend and copies the state. You should see output similar to the following:

Step 3 — Verify the migration

After the migration completes, verify that your S3 bucket contains the state object at the path specified by the key (for example prd/terraform.tfstate). If the S3 backend previously had no state, Terraform creates the object during the copy. Below is a screenshot showing the prd/terraform.tfstate object in the S3 console after migration.
Screenshot of an Amazon S3 console showing the "prd/" folder in a terraform state bucket with a single object named "terraform.tfstate" (11.4 KB). The listing shows its last-modified timestamp and the usual S3 action buttons (Copy S3 URI, Download, Upload, etc.).
Now Terraform will read and write state directly to S3 for subsequent operations.

Migrating back to the local backend

If you later decide to move state back from S3 to local, reverse the change: comment out (or remove) the backend configuration and re-run terraform init -migrate-state. Example of commenting the block:
Then:
After you confirm, Terraform copies the remote S3 state back into your local terraform.tfstate file. Note that Terraform copies state content to the currently configured backend; it does not automatically delete the source object (the S3 object remains unless you remove it manually).
Be careful when migrating state: copying state into a new backend replaces the destination backend’s state (if any) with the source state’s content. Make backups if you are unsure and ensure access controls are correct for remote state files because they can contain sensitive data.

Example: Truncated state file JSON

A portion of a Terraform state file (truncated for brevity):

Quick reference: Commands and common actions

Summary

To migrate Terraform state between backends:
  • Add or modify the terraform { backend "..." { } } block in your configuration.
  • Run terraform init -migrate-state to initialize the backend and copy state.
  • Confirm at the prompt whether to copy existing state to the destination backend.
  • Verify the state object exists in the remote backend (S3) or locally depending on the destination.
This process preserves your existing infrastructure management while moving where the state is stored.

Watch Video