github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/website/docs/cli/commands/state/mv.html.md (about)

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