When using the enhanced remote backend, a subset of all Terraform operations are supported. Of these, only plan and apply can be executed on the remote infrastructure (e.g. Terraform Cloud). Other operations run locally and use the remote backend for state storage. This causes problems when the local version of Terraform does not match the configured version from the remote workspace. If the two versions are incompatible, an `import` or `state mv` operation can cause the remote workspace to be unusable until a manual fix is applied. To prevent this from happening accidentally, this commit introduces a check that the local Terraform version and the configured remote workspace Terraform version are compatible. This check is skipped for commands which do not write state, and can also be disabled by the use of a new command-line flag, `-ignore-remote-version`. Terraform version compatibility is defined as: - For all releases before 0.14.0, local must exactly equal remote, as two different versions cannot share state; - 0.14.0 to 1.0.x are compatible, as we will not change the state version number until at least Terraform 1.1.0; - Versions after 1.1.0 must have the same major and minor versions, as we will not change the state version number in a patch release. If the two versions are incompatible, a diagnostic is displayed, advising that the error can be suppressed with `-ignore-remote-version`. When this flag is used, the diagnostic is still displayed, but as a warning instead of an error. Commands which will not write state can assert this fact by calling the helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the checks. Those which can write state should instead call the helper `meta.remoteBackendVersionCheck`, which will return diagnostics for display. In addition to these explicit paths for managing the version check, we have an implicit check in the remote backend's state manager initialization method. Both of the above helpers will disable this check. This fallback is in place to ensure that future code paths which access state cannot accidentally skip the remote version check.
4.7 KiB
layout, page_title, sidebar_current, description
| layout | page_title | sidebar_current | description |
|---|---|---|---|
| docs | Command: state mv | docs-commands-state-sub-mv | The `terraform state mv` command moves items in the Terraform state. |
Command: state mv
The terraform state mv command is used to move items in a
Terraform state. This command can move
single resources, single instances of a resource, entire modules, and more.
This command can also move items to a completely different state file,
enabling efficient refactoring.
Usage
Usage: terraform state mv [options] SOURCE DESTINATION
This command will move an item matched by the address given to the destination address. This command can also move to a destination address in a completely different state file.
This can be used for simple resource renaming, moving items to and from a module, moving entire modules, and more. And because this command can also move data to a completely new state, it can also be used for refactoring one configuration into multiple separately managed Terraform configurations.
This command will output a backup copy of the state prior to saving any changes. The backup cannot be disabled. Due to the destructive nature of this command, backups are required.
If you're moving an item to a different state file, a backup will be created for each state file.
This command requires a source and destination address of the item to move. Addresses are in resource addressing format.
The command-line flags are all optional. The list of available flags are:
-
-backup=path- Path where Terraform should write the backup for the original state. This can't be disabled. If not set, Terraform will write it to the same path as the statefile with a ".backup" extension. -
-backup-out=path- Path where Terraform should write the backup for the destination state. This can't be disabled. If not set, Terraform will write it to the same path as the destination state file with a backup extension. This only needs to be specified if -state-out is set to a different path than -state. -
-state=path- Path to the source state file to read from. Defaults to the configured backend, or "terraform.tfstate". -
-state-out=path- Path to the destination state file to write to. If this isn't specified the source state file will be used. This can be a new or existing path. -
-ignore-remote-version- When using the enhanced remote backend with Terraform Cloud, continue even if remote and local Terraform versions differ. This may result in an unusable Terraform Cloud workspace, and should be used with extreme caution.
Example: Rename a Resource
The example below renames the packet_device resource named worker to helper:
$ terraform state mv 'packet_device.worker' 'packet_device.helper'
Example: Move a Resource Into a Module
The example below moves the packet_device resource named worker into a module
named app. The module will be created if it doesn't exist.
$ terraform state mv 'packet_device.worker' 'module.app.packet_device.worker'
Example: Move a Module Into a Module
The example below moves the module named app under the module named parent.
$ terraform state mv 'module.app' 'module.parent.module.app'
Example: Move a Module to Another State
The example below moves the module named app into another state file. This removes
the module from the original state file and adds it to the destination.
The source and destination are the same meaning we're keeping the same name.
$ terraform state mv -state-out=other.tfstate 'module.app' 'module.app'
Example: Move a Resource configured with count
The example below moves the first instance of a packet_device resource named worker configured with
count to
the first instance of a resource named helper also configured with count:
$ terraform state mv 'packet_device.worker[0]' 'packet_device.helper[0]'
Example: Move a Resource configured with for_each
The example below moves the "example123" instance of a packet_device resource named worker configured with
for_each
to the "example456" instance of a resource named helper also configuring for_each:
Linux, Mac OS, and UNIX:
$ terraform state mv 'packet_device.worker["example123"]' 'packet_device.helper["example456"]'
PowerShell:
$ terraform state mv 'packet_device.worker[\"example123\"]' 'packet_device.helper[\"example456\"]'
Windows cmd.exe:
$ terraform state mv packet_device.worker[\"example123\"] packet_device.helper[\"example456\"]