Merging resources from several terraform states

2 min read

If you have several terraform projects for handling the deployment of a part of the application and you want to consolidate it into a single project, you can create a new terraform state and import all the resources using terraform import or you can use tfstate-merge to copy resources to the new state to consolidate it

Let's assume we have some resources on the infrastructure project and some others on the application that we want to copy it's state to the infrastructure_and_application project folder. We can merge he resources as follows:

$ python mergestates.py /home/pet2cattle/terraform/infrastructure /home/pet2cattle/terraform/application /home/pet2cattle/terraform/infrastructure_and_application 
Acquiring state lock. This may take a few moments...
Releasing state lock. This may take a few moments...

This tool is going to get the terraform state (terraform state pull / terraform state push) from all the three projects, then it's going to merge the resources from infrastructure and application into infrastructure_and_application. If any resources is duplicated the merge will fail:

$ python mergestates.py /home/pet2cattle/terraform/infrastructure /home/pet2cattle/terraform/application /home/pet2cattle/terraform/infrastructure_and_application 
Error reading source state "/tmp/tmpfdp7nj_p/merged": 3 problems:

- Duplicate resource instance in state: Instance data.aws_security_group.public_access appears multiple times in the state file.
Error pushing terraform state to /home/pet2cattle/terraform/infrastructure_and_application

So we will have to manually deal with the situation, datasources are allowed to be duplicated, so it will silently discard the datasources from the source project


Posted on 06/10/2021

Categories