Skip to content

migrate

Usage

Migrate OpenTofu/Terraform state from one unit to another.

Examples

Migrate backend state from `unit` to `unit-renamed`.

Terminal window
backend migrate old-unit-name new-unit-name

Force state migration, even if the bucket doesn't have versioning enabled.

Terminal window
backend migrate --force old-unit-name new-unit-name

This command will migrate the OpenTofu/Terraform state backend from one unit to another.

You will typically want to use this command if you are using a key attribute for your remote_state block that uses the path_relative_to_include function, and you want to rename the unit.

For example, given the following filesystem structure:

  • Directoryold-unit-name
    • terragrunt.hcl
  • root.hcl
root.hcl
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite"
}
config = {
bucket = "my-tofu-state"
key = "${path_relative_to_include()}/tofu.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "my-lock-table"
}
}
old-unit-name/terragrunt.hcl
include "root" {
path = find_in_parent_folders("root.hcl")
}

You couldn’t simply rename the old-unit-name directory to new-unit-name and run terragrunt apply in new-unit-name, because the change in the evaluated value for path_relative_to_include() would result in a new state key for the new-unit-name unit.

Instead, you can use the backend migrate command to migrate the backend state from the old-unit-name unit to the new-unit-name unit.

Terminal window
cp -R old-unit-name new-unit-name
terragrunt backend migrate old-unit-name new-unit-name
rm -rf old-unit-name

This will migrate the backend state from the old-unit-name unit to the new-unit-name unit, and then delete the old-unit-name unit.

Terragrunt performs migrations in one of two ways, depending on the level of support for the backends being migrated, and the state of configuration between the two units.

  1. When both units use the same supported backend (S3, GCS, or Azure Storage), Terragrunt uses the cloud provider’s SDK to move the state. For Azure Storage, both units must use the same storage account. backend migrate refuses cross-account moves and does not fall back to the OpenTofu/Terraform CLI.

    For a cross-account move, stop writes to both units and back up the source state. Initialize each backend, then copy the state with state pull and state push. Verify the destination state before deleting the source blob and retiring the old unit. Otherwise, later runs can update separate copies of the state.

    For GCS, Terragrunt moves the state of the default workspace, <prefix>/default.tfstate, and leaves other workspaces under the prefix in place. The two units can use different buckets, but Terragrunt makes the move with the source unit’s credentials, so those credentials need access to both buckets.

  2. If either backend source isn’t supported by Terragrunt, or the state of configuration between the two units is different, Terragrunt will instead use the OpenTofu/Terraform CLI to move the state between the two units. This is the fallback method, and will generally be slower. Terragrunt also won’t be able to delete the existing state from the source unit in this case, so you’ll need to handle that yourself.

Flags

--configoptional(string)

Path to the Terragrunt configuration file to use to migrate the resources. Note that this path is relative to the directory of each of the source and destination units, not the current working directory.

Environment Variables:

  • TG_CONFIG
--download-diroptional(string)

Path to download OpenTofu/Terraform modules into. The default is `.terragrunt-cache`. Note that this path is relative to the directory of each of the source and destination units, not the current working directory.

Environment Variables:

  • TG_DOWNLOAD_DIR
--forceoptional(bool)

When this flag is set, Terragrunt will force the migration of the backend state, even if the bucket containing it has versioning disabled.

Environment Variables:

  • TG_FORCE