Open FernandoMiguel opened 6 years ago
Hi @FernandoMiguel! Thanks for starting this discussion.
This definitely seems like a good feature. The challenge, as I'm sure you can imagine, is in figuring out what is the right workflow/UX for it, since operations like this don't fit into some of the usual assumptions that the CLI commands make:
I think there is a plausible design to be found here, but we'll need to spend some time designing and prototyping to figure out what makes sense. Since our focus is elsewhere at the time of writing (on the configuration language) I'm going to label this to remind us to look at it in more detail in the future.
In the mean time it's possible to achieve some refactoring use-cases using the terraform state mv
plumbing command. Its interface is somewhat clumsy due to the above constraints, but you can use it to move a single resource or an entire module into a new file:
$ terraform state mv -state-out=new.tfstate module.example module.example
The main limitation here is that this is a one-shot command that can only operate on one addressable object at a time, and so it's not well-suited to complex, multi-step refactoring tasks.
thanks for taking this into consideration. let's hope the future brings this to a finished implementation
Since this is 4 years old: are there any new, similar issues still under consideration? I would very much like to see a way to split state or move objects between remote state files.
Since this command isn't implemented, but the needs are still there, here's a short description of how I did it recently in a somewhat automated way using existing terraform commands (tested on terraform ver: 1.2.6).
Prerequisites:
High level overview of the steps:
module.name.aws_something.this
etc) and save it to some filecat $resources.txt | while read -r resource; do terraform state mv -state-out=new.tfstate "$resource" "$resource"; done
.
Do not use parallel or xargs - you will break JSON structure, this has to be done one-by-one. Take your time.This would be so handy to have as a feature!
Currently terraform split file looks as on https://support.hashicorp.com/hc/en-us/articles/7955227415059-How-to-Split-State-Files
but I see huge business case which can be very important.
We use a terraform to create primary and disaster recovery environment by defining second provider to other AWS region. It would be nice to have possibility to automatically split a state based on provider. This will be very crucial in case if one region of AWS will go down we still want to have management on secondary region
You could also use terraform-state-split
cli which helps a lot with the process in big scale projects.
https://github.com/shebang-labs/terraform-state-split
Demo: https://asciinema.org/a/qqF2E5Uz2ybwzhJdMpuufzblu
Install:
brew tap shebang-labs/tap
brew install terraform-state-split
Another tool capable of splitting one Terraform root module in two (split after and split before) is https://github.com/pix4D/terravalet (by me).
I would like to start discussion on adding a new terraform state command that would allow users to split statefiles into new smaller statefiles
the idea is that when a statefile grows too big, TF could manage to split those resources , moving some resources to a new statefile (and maybe update the corresponding backend ? )
Is this something terraform core could do in the future?