terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Cannot terraform state mv resources from module to root

Open navaati opened this issue 8 years ago • 9 comments

Hi.

I cannot move all resources in a module to the root module with terraform state mv. Actually there is no syntax available to do so. I tried to do terraform state mv module.mymod root or terraform state mv module.mymod . or even terraform state mv module.mymod ' ' or terraform state mv module.mymod '' but that didn't cut it.

I finally resolved to do it this way: terraform state pull |jq '.modules |= map(select(.path |length > 1) |.path |= (["root"] + .[2:]))' |terraform state push /dev/stdin, and it worked ! But that felt like a dangerous game.

To be fair, I don't totally know if this is a bug of a feature request :).

Terraform Version

Terraform v0.10.8

Cheers.

navaati avatar Nov 10 '17 16:11 navaati

Do we know if this is possible today?

I'm looking to move a module from 'module.X.module.Y' to 'module.Y' and I cannot.

svaranasi-corporate avatar Apr 06 '20 22:04 svaranasi-corporate

FWIW, I have been able to move resources to the root module by simply removing the module.x specifier when doing the terraform state mv (i.e. I just used the resource's type/name as the "target"). What command did you try? What version of Terraform are you using? What is the output of terraform state mv module.X.module.Y module.Y? You may want to use a different name for the "target" just in case, because you are designating that two identically named modules exist at different nesting levels which maybe causes an issue? That is speculation, but it may give you some traction if you move instead to module.Z first, do a state rm of the old module.Y, then rename your module.Z back to module.Y.

trifleneurotic avatar May 03 '20 18:05 trifleneurotic

@landgazr That's fine if you want to move a single resource, but won't let you move the contents of an entire module

smiller171 avatar May 28 '20 17:05 smiller171

I think the only way to handle this use-case right now is to write a script that generates all of the mv commands. I don't see any magical "root" marker that can be used as a destination arg in the source. https://github.com/hashicorp/terraform/blob/master/command/state_mv.go

I finally resolved to do it this way: terraform state pull |jq '.modules |= map(select(.path |length > 1) |.path |= (["root"] + .[2:]))' |terraform state push /dev/stdin, and it worked ! But that felt like a dangerous game.

yeah, I'm sure this isn't generally advisable. in 0.12+ at least, .modules doesn't exist in my tf state pull output but even if it did, this would miss nested providers and dependencies which both also encode the module paths. that being said, I feel your pain. I'm looking at generating 1900 mv commands because of this; even with -lock=false I believe this will take an extremely long time (maybe 11 hours?).

jerryhebert avatar Jun 11 '20 06:06 jerryhebert

I was able to move a large module to root using this

terraform state list | 
  grep module.old_module | 
  cut -d. -f 3- | 
  xargs -I {} terraform state mv module.old_module.{} {}

so-jelly avatar Mar 02 '21 13:03 so-jelly

I was able to use terraform-state-mover to easily move states from modules to root in a small GCE instance on google cloud. It would probably work well for larger configs too

syntapy avatar Jul 26 '21 01:07 syntapy

I didn't get @so-jelly's answer to work out of the box (using Mac, not sure if relevant), because I needed some quoting done on resources like module.old_module.foo["my-resource-A"]. I was able to get it working using a sed step which escapes " as well and piping the output to a file:

terraform state list |
grep module.old_module | cut -d. -f 3- | sed 's/\"/\\\"/g' |
xargs -I {} echo terraform state mv \'module.old_module.{}\' \'{}\' > commands_to_run.sh

Then I run sh commands_to_run.sh and it works like a charm

apamildner avatar Mar 17 '23 09:03 apamildner

To iterate on the work done by others in this issue I created a gist for generating moved blocks rather than generating terraform state mv commands https://gist.github.com/audunsolemdal/07bda02616dbd8ceacc23ba55a113da7

This is useful when moving a lot of resources, as state mv is run for one resource at a time.

audunsolemdal avatar Oct 10 '23 10:10 audunsolemdal

just commenting here to show support for "this should be an inbuilt feature"

tarfeef101 avatar May 23 '25 06:05 tarfeef101