skip to Main Content

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


  1. Yes, you can by moving resources in the terraform.tfstate, it is really complicated but not impossible.

    To do so, you need to:

    1. list your resources in your actual project, using for example terraform state list
    2. create another project(s), based on your logic
    3. each project should have a tfstate
    4. now you can move resources using for example: terraform state mv -state-out=../child/terraform.tfstate module.xxxx module.xxx
    5. move the script of your resources from the parent to child project
    6. then plan & apply your projects

    Maybe I forgot some steps, but these are the big lines to solve your problem(I guess).

    Login or Signup to reply.
  2. 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:

    moved {
      from = module.security_group.aws_security_group.sg_8080
      to   = module.web_security_group.aws_security_group.this[0]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search