I currently have a project that has grown quite a bit and I wonder if it is possible to break it down into small elements without the need to destroy everything that has been deployed in the cloud, I have worked with modules for lambda, event bridge, api gateway for example.
im using terraform Terraform v1.2.8 on darwin_amd64
ps: with break down into small elements i mean several new terraform projects derived from the monolith, the main reason is to automate the build process into a pipeline by components approach.
Thanks.
2
Answers
Yes, you can by moving resources in the
terraform.tfstate
, it is really complicated but not impossible.To do so, you need to:
terraform state list
terraform state mv -state-out=../child/terraform.tfstate module.xxxx module.xxx
Maybe I forgot some steps, but these are the big lines to solve your problem(I guess).
Youcef’s answer above is accurate, however it’s also a bit outdated depending on your terraform version.
HashiCorp introduced the moved block configuration in one of the last few releases to help with this exact issue. Here’s a link to a step-by-step guide on what this process is like:
https://developer.hashicorp.com/terraform/tutorials/configuration-language/move-config#move-your-resources-with-the-moved-configuration-block
It looks like this: