docs/src/data/commands/backend/migrate.mdx
import FileTree from '@components/vendored/starlight/FileTree.astro';
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:
<FileTree># 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.
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.