github.com/pulumi/terraform@v1.4.0/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 the [Terraform Cloud CLI integration](/cli/cloud) or the [`remote` backend](/language/settings/backends/remote) 70 only, `terraform state mv` 71 also accepts the option 72 [`-ignore-remote-version`](/cli/cloud/command-line-arguments#ignore-remote-version). 73 74 The legacy options [`-backup` and `-backup-out`](/language/settings/backends/local#command-line-arguments) 75 operate on a local state file only. Configurations using 76 [the `remote` backend](/language/settings/backends/remote) 77 must specify a local state file with the [`-state`](/language/settings/backends/local#command-line-arguments) 78 option in order to use the [`-backup` and `-backup-out`](/language/settings/backends/local#command-line-arguments) 79 options. 80 81 For configurations using 82 [the `local` state mv](/language/settings/backends/local) only, 83 `terraform state mv` also accepts the legacy options 84 [`-state`, `-state-out`, `-backup`, and `-backup-out`](/language/settings/backends/local#command-line-arguments). 85 86 ## Example: Rename a Resource 87 88 Renaming a resource means making a configuration change like the following: 89 90 ```diff 91 -resource "packet_device" "worker" { 92 +resource "packet_device" "helper" { 93 # ... 94 } 95 ``` 96 97 To tell Terraform that it should treat the new "helper" resource as a rename 98 of the old "worker" resource, you can pair the above configuration change 99 with the following command: 100 101 ```shell 102 terraform state mv packet_device.worker packet_device.helper 103 ``` 104 105 ## Example: Move a Resource Into a Module 106 107 If you originally wrote a resource in your root module but now wish to refactor 108 it into a child module, you can move the `resource` block into the child 109 module configuration, removing the original in the root module, and then 110 run the following command to tell Terraform to treat it as a move: 111 112 ```shell 113 terraform state mv packet_device.worker module.worker.packet_device.worker 114 ``` 115 116 In the above example the new resource has the same name but a different module 117 address. You could also change the resource name at the same time, if the new 118 module organization suggests a different naming scheme: 119 120 ```shell 121 terraform state mv packet_device.worker module.worker.packet_device.main 122 ``` 123 124 ## Example: Move a Module Into a Module 125 126 You can also refactor an entire module into a child module. In the 127 configuration, move the `module` block representing the module into a different 128 module and then pair that change with a command like the following: 129 130 ```shell 131 terraform state mv module.app module.parent.module.app 132 ``` 133 134 ## Example: Move a Particular Instance of a Resource using `count` 135 136 A resource defined with [the `count` meta-argument](/language/meta-arguments/count) 137 has multiple instances that are each identified by an integer. You can 138 select a particular instance by including an explicit index in your given 139 address: 140 141 ```shell 142 $ terraform state mv 'packet_device.worker[0]' 'packet_device.helper[0]' 143 ``` 144 145 A resource that doesn't use `count` or `for_each` has only a single resource 146 instance whose address is the same as the resource itself, and so you can 147 move from an address not containing an index to an address containing an index, 148 or the opposite, as long as the address type you use matches whether and how 149 each resource is configured: 150 151 ```shell 152 $ terraform state mv 'packet_device.main' 'packet_device.all[0]' 153 ``` 154 155 Brackets (`[`, `]`) have a special meaning in some shells, so you may need to 156 quote or escape the address in order to pass it literally to Terraform. 157 The above examples show the typical quoting syntax for Unix-style shells. 158 159 ## Example: Move a Resource configured with for_each 160 161 A resource defined with [the `for_each` meta-argument](/language/meta-arguments/for_each) 162 has multiple instances that are each identified by an string. You can 163 select a particular instance by including an explicit key in your given 164 address. 165 166 However, the syntax for strings includes quotes and the quote symbol often 167 has special meaning in command shells, so you'll need to use the appropriate 168 quoting and/or escaping syntax for the shell you are using. For example: 169 170 Unix-style shells, such as on Linux or macOS: 171 172 ```shell 173 terraform state mv 'packet_device.worker["example123"]' 'packet_device.helper["example456"]' 174 ``` 175 176 Windows Command Prompt (`cmd.exe`): 177 178 ```shell 179 terraform state mv packet_device.worker[\"example123\"] packet_device.helper[\"example456\"] 180 ``` 181 182 PowerShell: 183 184 ```shell 185 terraform state mv 'packet_device.worker[\"example123\"]' 'packet_device.helper[\"example456\"]' 186 ``` 187 188 Aside from the use of strings instead of integers for instance keys, the 189 treatment of `for_each` resources is similar to `count` resources and so 190 the same combinations of addresses with and without index components is 191 valid as described in the previous section.