github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/state/mv.mdx (about) 1 --- 2 page_title: 'Command: state mv' 3 description: >- 4 The `terraform state mv` command changes bindings in Terraform state, 5 associating existing remote objects with new resource instances. 6 --- 7 8 # Command: state mv 9 10 The main function of [Terraform state](/language/state) is 11 to track the bindings between resource instance addresses in your configuration 12 and the remote objects they represent. Normally Terraform automatically 13 updates the state in response to actions taken when applying a plan, such as 14 removing a binding for an remote object that has now been deleted. 15 16 You can use `terraform state mv` in the less common situation where you wish 17 to retain an existing remote object but track it as a different resource 18 instance address in Terraform, such as if you have renamed a resource block 19 or you have moved it into a different module in your configuration. 20 21 ## Usage 22 23 Usage: `terraform state mv [options] SOURCE DESTINATION` 24 25 Terraform will look in the current state for a resource instance, resource, 26 or module that matches the given address, and if successful it will move the 27 remote objects currently associated with the source to be tracked instead 28 by the destination. 29 30 Both the source and destination addresses must use 31 [resource address syntax](/cli/state/resource-addressing), and 32 they must both refer to the same kind of object: you can only move a resource 33 instance to another resource instance, a whole module instance to another 34 whole module instance, etc. Furthermore, if you are moving a resource or 35 a resource instance then you can only move it to a new address with the 36 same resource type. 37 38 The most common uses for `terraform state mv` are when you have renamed a 39 resource block in your configuration or you've moved a resource block into 40 a child module, in both cases with the intention of retaining the existing 41 object but tracking it under a new name. By default Terraform will understand 42 moving or renaming a resource configuration as a request to delete the old 43 object and create a new object at the new address, and so `terraform state mv` 44 allows you to override that interpretation by pre-emptively attaching the 45 existing object to the new address in Terraform. 46 47 ~> _Warning:_ If you are using Terraform in a collaborative environment, you 48 must ensure that when you are using `terraform state mv` for a code refactoring 49 purpose you communicate carefully with your coworkers to ensure that nobody 50 makes any other changes between your configuration change and your 51 `terraform state mv` command, because otherwise they might inadvertently create 52 a plan that will destroy the old object and create a new object at the new 53 address. 54 55 This command also accepts the following options: 56 57 * `-dry-run` - Report all of the resource instances that match the given 58 address without actually "forgetting" any of them. 59 60 * `-lock=false` - Don't hold a state lock during the operation. This is 61 dangerous if others might concurrently run commands against the same 62 workspace. 63 64 * `-lock-timeout=DURATION` - Unless locking is disabled with `-lock=false`, 65 instructs Terraform to retry acquiring a lock for a period of time before 66 returning an error. The duration syntax is a number followed by a time 67 unit letter, such as "3s" for three seconds. 68 69 For configurations using 70 [the `remote` backend](/language/settings/backends/remote) 71 only, `terraform state mv` 72 also accepts the option 73 [`-ignore-remote-version`](/language/settings/backends/remote#command-line-arguments). 74 75 The legacy options [`-backup` and `-backup-out`](/language/settings/backends/local#command-line-arguments) 76 operate on a local state file only. Configurations using 77 [the `remote` backend](/language/settings/backends/remote) 78 must specify a local state file with the [`-state`](/language/settings/backends/local#command-line-arguments) 79 option in order to use the [`-backup` and `-backup-out`](/language/settings/backends/local#command-line-arguments) 80 options. 81 82 For configurations using 83 [the `local` state mv](/language/settings/backends/local) only, 84 `terraform state mv` also accepts the legacy options 85 [`-state`, `-state-out`, `-backup`, and `-backup-out`](/language/settings/backends/local#command-line-arguments). 86 87 ## Example: Rename a Resource 88 89 Renaming a resource means making a configuration change like the following: 90 91 ```diff 92 -resource "packet_device" "worker" { 93 +resource "packet_device" "helper" { 94 # ... 95 } 96 ``` 97 98 To tell Terraform that it should treat the new "helper" resource as a rename 99 of the old "worker" resource, you can pair the above configuration change 100 with the following command: 101 102 ```shell 103 terraform state mv packet_device.worker packet_device.helper 104 ``` 105 106 ## Example: Move a Resource Into a Module 107 108 If you originally wrote a resource in your root module but now wish to refactor 109 it into a child module, you can move the `resource` block into the child 110 module configuration, removing the original in the root module, and then 111 run the following command to tell Terraform to treat it as a move: 112 113 ```shell 114 terraform state mv packet_device.worker module.worker.packet_device.worker 115 ``` 116 117 In the above example the new resource has the same name but a different module 118 address. You could also change the resource name at the same time, if the new 119 module organization suggests a different naming scheme: 120 121 ```shell 122 terraform state mv packet_device.worker module.worker.packet_device.main 123 ``` 124 125 ## Example: Move a Module Into a Module 126 127 You can also refactor an entire module into a child module. In the 128 configuration, move the `module` block representing the module into a different 129 module and then pair that change with a command like the following: 130 131 ```shell 132 terraform state mv module.app module.parent.module.app 133 ``` 134 135 ## Example: Move a Particular Instance of a Resource using `count` 136 137 A resource defined with [the `count` meta-argument](/language/meta-arguments/count) 138 has multiple instances that are each identified by an integer. You can 139 select a particular instance by including an explicit index in your given 140 address: 141 142 ```shell 143 $ terraform state mv 'packet_device.worker[0]' 'packet_device.helper[0]' 144 ``` 145 146 A resource that doesn't use `count` or `for_each` has only a single resource 147 instance whose address is the same as the resource itself, and so you can 148 move from an address not containing an index to an address containing an index, 149 or the opposite, as long as the address type you use matches whether and how 150 each resource is configured: 151 152 ```shell 153 $ terraform state mv 'packet_device.main' 'packet_device.all[0]' 154 ``` 155 156 Brackets (`[`, `]`) have a special meaning in some shells, so you may need to 157 quote or escape the address in order to pass it literally to Terraform. 158 The above examples show the typical quoting syntax for Unix-style shells. 159 160 ## Example: Move a Resource configured with for_each 161 162 A resource defined with [the `for_each` meta-argument](/language/meta-arguments/for_each) 163 has multiple instances that are each identified by an string. You can 164 select a particular instance by including an explicit key in your given 165 address. 166 167 However, the syntax for strings includes quotes and the quote symbol often 168 has special meaning in command shells, so you'll need to use the appropriate 169 quoting and/or escaping syntax for the shell you are using. For example: 170 171 Unix-style shells, such as on Linux or macOS: 172 173 ```shell 174 terraform state mv 'packet_device.worker["example123"]' 'packet_device.helper["example456"]' 175 ``` 176 177 Windows Command Prompt (`cmd.exe`): 178 179 ```shell 180 terraform state mv packet_device.worker[\"example123\"] packet_device.helper[\"example456\"] 181 ``` 182 183 PowerShell: 184 185 ```shell 186 terraform state mv 'packet_device.worker[\"example123\"]' 'packet_device.helper[\"example456\"]' 187 ``` 188 189 Aside from the use of strings instead of integers for instance keys, the 190 treatment of `for_each` resources is similar to `count` resources and so 191 the same combinations of addresses with and without index components is 192 valid as described in the previous section.