github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/commands/state/mv.html.md (about) 1 --- 2 layout: "commands-state" 3 page_title: "Command: state mv" 4 sidebar_current: "docs-commands-state-sub-mv" 5 description: |- 6 The `terraform state mv` command moves items in the Terraform state. 7 --- 8 9 # Command: state mv 10 11 The `terraform state mv` command is used to move items in a 12 [Terraform state](/docs/state/index.html). This command can move 13 single resources, single instances of a resource, entire modules, and more. 14 This command can also move items to a completely different state file, 15 enabling efficient refactoring. 16 17 ## Usage 18 19 Usage: `terraform state mv [options] SOURCE DESTINATION` 20 21 This command will move an item matched by the address given to the 22 destination address. This command can also move to a destination address 23 in a completely different state file. 24 25 This can be used for simple resource renaming, moving items to and from 26 a module, moving entire modules, and more. And because this command can also 27 move data to a completely new state, it can also be used for refactoring 28 one configuration into multiple separately managed Terraform configurations. 29 30 This command will output a backup copy of the state prior to saving any 31 changes. The backup cannot be disabled. Due to the destructive nature 32 of this command, backups are required. 33 34 If you're moving an item to a different state file, a backup will be created 35 for each state file. 36 37 This command requires a source and destination address of the item to move. 38 Addresses are 39 in [resource addressing format](/docs/commands/state/addressing.html). 40 41 The command-line flags are all optional. The list of available flags are: 42 43 * `-backup=path` - Path where Terraform should write the backup for the 44 original state. This can't be disabled. If not set, Terraform will write it 45 to the same path as the statefile with a ".backup" extension. 46 47 * `-backup-out=path` - Path where Terraform should write the backup for the 48 destination state. This can't be disabled. If not set, Terraform will write 49 it to the same path as the destination state file with a backup extension. 50 This only needs to be specified if -state-out is set to a different path than 51 -state. 52 53 * `-state=path` - Path to the source state file to read from. Defaults to the 54 configured backend, or "terraform.tfstate". 55 56 * `-state-out=path` - Path to the destination state file to write to. If this 57 isn't specified the source state file will be used. This can be a new or 58 existing path. 59 60 ## Example: Rename a Resource 61 62 The example below renames the `packet_device` resource named `worker` to `helper`: 63 64 ```shell 65 $ terraform state mv 'packet_device.worker' 'packet_device.helper' 66 ``` 67 68 ## Example: Move a Resource Into a Module 69 70 The example below moves the `packet_device` resource named `worker` into a module 71 named `app`. The module will be created if it doesn't exist. 72 73 ```shell 74 $ terraform state mv 'packet_device.worker' 'module.app' 75 ``` 76 77 ## Example: Move a Module Into a Module 78 79 The example below moves the module named `app` under the module named `parent`. 80 81 ```shell 82 $ terraform state mv 'module.app' 'module.parent.module.app' 83 ``` 84 85 ## Example: Move a Module to Another State 86 87 The example below moves the module named `app` into another state file. This removes 88 the module from the original state file and adds it to the destination. 89 The source and destination are the same meaning we're keeping the same name. 90 91 ```shell 92 $ terraform state mv -state-out=other.tfstate 'module.app' 'module.app' 93 ``` 94 95 ## Example: Move a Resource configured with count 96 97 The example below moves the first instance of a `packet_device` resource named `worker` configured with 98 [`count`](/docs/configuration/resources.html#count-multiple-resource-instances-by-count) to 99 the first instance of a resource named `helper` also configured with `count`: 100 101 ```shell 102 $ terraform state mv 'packet_device.worker[0]' 'packet_device.helper[0]' 103 ``` 104 105 ## Example: Move a Resource configured with for_each 106 107 The example below moves the `"example123"` instance of a `packet_device` resource named `worker` configured with 108 [`for_each`](/docs/configuration/resources.html#for_each-multiple-resource-instances-defined-by-a-map-or-set-of-strings) 109 to the `"example456"` instance of a resource named `helper` also configuring `for_each`: 110 111 Linux, Mac OS, and UNIX: 112 113 ```shell 114 $ terraform state mv 'packet_device.worker["example123"]' 'packet_device.helper["example456"]' 115 ``` 116 117 PowerShell: 118 119 ```shell 120 $ terraform state mv 'packet_device.worker[\"example123\"]' 'packet_device.helper[\"example456\"]' 121 ``` 122 123 Windows `cmd.exe`: 124 125 ```shell 126 $ terraform state mv packet_device.worker[\"example123\"] packet_device.helper[\"example456\"] 127 ```