terraform state mv command moves items in the Terraform state, allowing you to rename resources or move them between modules.
Synopsis
Move an item in the state.Usage
Options
If set, prints out what would’ve been moved but doesn’t actually move anything.
Don’t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.
Duration to retry a state lock.
A rare option used for the remote backend only. See the remote backend documentation for more information.
Path to the source state file. Legacy option supported for the local backend only.
Path to the destination state file. Legacy option supported for the local backend only.
Path where Terraform should write the backup for the source state. Legacy option supported for the local backend only.
Path where Terraform should write the backup for the destination state. Legacy option supported for the local backend only.
Arguments
The source address of the resource to move. Can be a resource instance, entire resource, or module.
The destination address where the resource should be moved.
Examples
Rename a resource
Rename a resource from one name to another:Move a resource into a module
Move a resource from the root module into a child module:Move a resource out of a module
Move a resource from a module to the root:Move an entire module
Move all resources from one module to another:Move resource to different state file
Move a resource to a different state file:Dry run
Preview what would be moved without making changes:Move indexed resource
Move a resource instance with an index:Move to resource with count
Move a single instance to become part of a counted resource:Common Use Cases
Refactor resource names
When renaming resources in your configuration, usestate mv to update the state to match without destroying and recreating the resources.
Reorganize modules
Move resources into or out of modules as your infrastructure organization evolves.Split configurations
When splitting a monolithic Terraform configuration into multiple separate configurations, usestate mv with -state-out to move resources between state files.
Add or remove count/for_each
When addingcount or for_each to an existing resource, use state mv to move the single instance to the appropriate indexed location.