Skip to content

Migrate State to a Remote Backend

If you’ve been using local state and want to migrate to a remote backend, the process involves a few steps. It’s important to be cautious when migrating state, as improper handling can result in losing or corrupting your state file.

Update your terraform block in the configuration to define the remote backend. For example, you could configure the backend as follows:

terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-west-2"
}
}

Once the remote backend is defined in your configuration, run:

Terminal window
terraform init

During initialization, Terraform will detect that you’re changing from a local backend to a remote backend. It will prompt you to confirm the migration.

Terraform will ask you to confirm if you want to migrate the state from local to the remote backend. If you confirm, it will upload your current local state file to the remote backend.

After migration, you can verify that your state file is stored remotely by running:

Terminal window
terraform state list

This should return the list of resources from the remote backend. Also, check the remote storage (e.g., S3 bucket) to ensure the state file has been uploaded.