Skip to content

Way to move resources from one state to another.  #32777

Description

@tmccombs

Terraform Version

any

Use Cases

Consider the problem of splitting up a monolithic terraform configuration into smaller root modules. This requires either splitting up the existing state, or tearing down and rebuilding all resources. In many cases the latter isn't possible. See for example https://medium.com/@calinflorescu3/splitting-a-monolithic-terraform-state-d26e866de629

Another possible use case is if you are refactoring something, and want to move a resource from one root module to a different one.

Attempted Solutions

There are a couple of ways you can split up the state currently, but they aren't great. And in particular involve doing things that terraform documentation explicitly recommends against.

One is you can manually edit the state files, or write scripts to modify the state files. But there is relatively little documentation of the format of the state files, and the documentation states "direct file editing of the state is discouraged."

Another option is to do a state pull on both states, then do a state mv using the --state and --state-out options, followed by using state push. This was the method used in the blog post above. But again the terraform documantion says the --state and --state-out options are "legacy features ... that we no longer recommend". And it is unclear that these options will be supported in future versions.

Finally, it is possible to do a state rm on the source state and an import on the destination state for each resource. However, not all resources support import. And there isn't currently a way to do bulk imports, which can make the import portion pretty slow.

Proposal

One option is not to change any functionality, but "bless" one of the first two above methods for moving resources between different states. Meaning, that rather than having a blanket recommendation against modifying state directly or using the legacy options, the documentation states cases where it is ok to use them (like moving resources between state).

Alternatively either change the existing state mv command or add a new command that can move a resource from one state to another, even for non-local backends. I'm not sure exactly how you would specify the options for the backends. Perhaps give a reference to another file (or directory?) containing the configuration for the destination backend? Ideally this option would also support doing a bulk operation so that many resources/modules could be moved at once.

References

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions