0.11.14
terraform state mv -state-out=../otherProject/terraform.tfstate module.source module.destination should move desired module form origin's S3 backend to the other project's statefile if it's placed locally, or ideally, even if it is stored in S3 as well.
State is removed from origin, but not moved to destination state.
terraform state mv -state-out=../otherProject/terraform.tfstate module.source module.destinationCurrent workaround, basically downloading the statefiles, apply the command on the locally, and then moving them back to the bucket: https://github.com/hashicorp/terraform/pull/15652#issuecomment-410754814
Hi @dimisjim , I'm sorry you've come across this odd behavior!
Can you share the debug output from your terraform state mv command? I'm curious if any errors are getting buried (check for sensitive data in the output, first).
I think we saw in another issue that the -state and -state-out arguments are arguments just to the local backend and not to Terraform itself, so whenever any other backend is used they have no effect.
In the short term it'd be good to find some way to make Terraform report an error in that case, rather than to silently do something unexpected/harmful.
I do agree that it would be better for the presence of -state and/or -state-out to override to use local state storage for only one part of the operation as expected here, to enable this sort of local-to-remote or remote-to-local refactoring. That will require some significant refactoring of how Terraform approaches state storage though -- probably to separate it more completely from the "backend" idea into a separate concept -- and thus I expect we won't be able to tackle that until we're doing a more general refactor of the backend and workspaces concepts we've been discussing, to simplify the user experience for them; that way we can get the necessary breaking changes out of the way all at once, to create less churn for those who are relying on the current behaviors.
@apparentlymart Thanks for the answer.
I think I will go ahead with the workaround linked in my initial post for the time being.
Running into same issue today. Pretty frustrating.
Yes same issue for me when I have to move state from one remote S3 backend to an other.
I do a similar workaround inspired of this https://medium.com/@lynnlin827/moving-terraform-resources-states-from-one-remote-state-to-another-c76f8b76a996.
There is also apparently no way to move from a local state file into remote state.
I encountered this bug, too, with GCS remote backends, and it's most irritating. Moving state between two remote backends is scary enough, even more so when the preferred tool to validate changes before they're actually pushed (terraform state) doesn't actually respect the local flags.
My process (also following https://medium.com/@lynnlin827/moving-terraform-resources-states-from-one-remote-state-to-another-c76f8b76a996) was:
dev/ to prod/ (These are separate TF environments, that know nothing about each other, and each have their own remote state buckets.) dev$> terraform state pull > tfstate && cp tfstate tfstate.bakprod$> terraform state pull > tfstate && cp tfstate tfstate.bakdev$> terraform state list -state=tfstate | grep <resource> (No results)prod$> terraform state list -state=tfstate | grep <resource> (No results)dev$> diff tfstate tfstate.bak (shows state diff)prod$> diff tfstate tfstate.bak (shows state diff)terraform state list command doesn't see the resources.dev$> terraform state push tfstatedev$> terraform plan (Shows no diff, as expected. The config for the moved resource has already been removed from dev and put in prod.)terraform state list -state=tfstate just isn't working properly, push to prod and run a plan.prod$> terraform state push tfstateprod$> terraform plan (No Diff, as expected)All-in-all, the fact that terraform commands _SILENTLY_ don't respect the -state flag when remote backends are enabled leaves me feeling like remote backends need a lot of work.
Ideally, moving between remotes would be trivial, but in the absence of that, I expect that the normal terraform state commands should work normally, and not add to the already-stressful confusion that can be part of moving state.
Most helpful comment
I think we saw in another issue that the
-stateand-state-outarguments are arguments just to thelocalbackend and not to Terraform itself, so whenever any other backend is used they have no effect.In the short term it'd be good to find some way to make Terraform report an error in that case, rather than to silently do something unexpected/harmful.
I do agree that it would be better for the presence of
-stateand/or-state-outto override to use local state storage for only one part of the operation as expected here, to enable this sort of local-to-remote or remote-to-local refactoring. That will require some significant refactoring of how Terraform approaches state storage though -- probably to separate it more completely from the "backend" idea into a separate concept -- and thus I expect we won't be able to tackle that until we're doing a more general refactor of the backend and workspaces concepts we've been discussing, to simplify the user experience for them; that way we can get the necessary breaking changes out of the way all at once, to create less churn for those who are relying on the current behaviors.