How to merge multiple tf state file while requesting 'plan' command

2,774 views
Skip to first unread message

je...@srcclr.com

unread,
Sep 14, 2017, 10:11:55 PM9/14/17
to Terraform
I have multiple modules section in separate folders.

Folder 1 - Module A

Folder 2 - Module B( takes Module A's output as input)


When i do 'terraform plan' inside folder 1, it always shows something to change because the resource created in module A has been updated in module B section also.
So is there a way i can ask terraform to look into module B tf file also while im doing 'terrraform plan' in folder 1

Anshu Prateek

unread,
Sep 14, 2017, 10:45:36 PM9/14/17
to Terraform
The resource should be created/managed in one place. It can call both modules in the same place.

For example, your code structure could look like this:

Modules
|- Module A
|- Module B
Providers
|- resourceA.tf

In resourceA.tf

module objA {
  source = "../modules/moduleA"
...
}

module objB {
  source = "../modules/moudleB"
 inputB2 = "${module.objA.someoutputablevar}"
}



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/8293f879-f751-4ccb-83c5-614b2d00e813%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
regards
Anshu Prateek
+91.991.610.2967

je...@srcclr.com

unread,
Sep 14, 2017, 11:03:15 PM9/14/17
to Terraform
Thanks Anshu.

I have already created the resource, state files are stored in separate folders. I can combine the modules but my concern is how to merge the tf state files into a single file.

Anshu Prateek

unread,
Sep 14, 2017, 11:09:04 PM9/14/17
to Terraform
Create a new temp folder with a new combined resource definition. Import your resource and ensure that there is no diff after import. This gives you a new combined state file. You can now move this statefile to any of your older locations (or discard your older ones and just keep the new one).

Trying to manually edit/merge the state file will be cumbersome and error prone.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

je...@srcclr.com

unread,
Sep 17, 2017, 11:35:49 PM9/17/17
to Terraform
Im having issues even after combining the modules.

--- Single tf file ---

   Module A - creates AWS VPC, route tables
   Module B - creates another VPC and its respective route tables
   
   Module C - Takes creates VPC Peering between ModA and ModB VPCs. Adds more rows to the route tables created in the Module A and Module B.
------------------------

Problem - When i do 'terraform plan',  it shows the resource needs updates 
                 ModuleA.XYZ.Route_tables
                 ModuleB.XYZ.Route_tables

               I understand this is because those tables are updated in the resource under Module C. Is there a way to overcome this

Anshu Prateek

unread,
Sep 18, 2017, 1:40:19 AM9/18/17
to Terraform
For the same usecase, we have separated out route tables in its own module.

Wrapper "network" module
sub - module A - create bare VPC
sub - module B - create subnets
sub - module C - create route tables.

within route table module, "peering" routes are one of the resources being created via the calling module parameters.



For more options, visit https://groups.google.com/d/optout.

je...@srcclr.com

unread,
Sep 18, 2017, 2:49:50 AM9/18/17
to Terraform
Let me break down the setup,

 Module C 
     - Created route table resource [ Terraform resource 'a' ]
     - For peering, adding rows to the route table created [ Terraform resource 'b'']
 Module D
     -  For more peering, adding more rows to the route table [ Terraform resource 'c']

Now 'terraform plan' complaints for the resources, 'a' and 'b'
        b - complaints, coz the route table has more rows that came from 'c'
        a - complaints, coz the route table has more rows that came from 'c' and 'b'
Reply all
Reply to author
Forward
0 new messages