Using Terraform config to create multiple stacks

693 views
Skip to first unread message

Amit Chauhan

unread,
Nov 17, 2015, 10:50:09 AM11/17/15
to Terraform
Hi All,

How can I use the same terraform tf file, with resource entries, to create different stacks for different applications?

For eg: lets say I have two application, App1 and App2 which need to be deployed to AWS. My terraform file is in /infra/aws/AppStack.tf

AppStack.tf file constains ec2 and elb resource with all parameters passed through variables. While provisioning the stack for App1, I would change the input variables and call Terraform apply in /infra/aws/AppStack.tf. I want to do same for App2. However because of the TFState file already in /infra/aws folder, it will not allow me to do so and it will actually update the App1 Stack.  Note that even remote Terraform state wont help here because it will also point to same resource.

How these types of tasks are normally done in Terraform where the need is to use the same TF file but provision different stacks for different applications. Here each application like App1 and App2 keep their own config in some format which just needs to be translated to the terraform input variables. After that Terraform should just create the Stack.

I can think of 2 ways to achieve this:

1) Delete TF state file after each run, so that in the folder we do not have TFState and each apply will create a new Stack for the different App. 

2) Read App config files and generate (programmatically) TF files in respective folders. For eg: TF file each in Applications/App1 and Applications/App2 and than run apply by giving DIR.

but both these approaches have issues and they don't seem very natural. Am i missing something obvious?

thanks
Amit

David Maze

unread,
Nov 17, 2015, 12:23:57 PM11/17/15
to Terraform
On Tuesday, November 17, 2015 at 10:50:09 AM UTC-5, Amit Chauhan wrote:
For eg: lets say I have two application, App1 and App2 which need to be deployed to AWS. My terraform file is in /infra/aws/AppStack.tf

AppStack.tf file constains ec2 and elb resource with all parameters passed through variables. While provisioning the stack for App1, I would change the input variables and call Terraform apply in /infra/aws/AppStack.tf. I want to do same for App2. However because of the TFState file already in /infra/aws folder, it will not allow me to do so and it will actually update the App1 Stack.  Note that even remote Terraform state wont help here because it will also point to same resource.

Look at the Terraform documentation about "modules".

You actually probably have fairly little to set up.  From what you've described, I think it's close to enough to create a /infra/App1/AppStack.tf file containing something like

module "AppStack" {
        source = "../aws"
        version = "1.2.3"
        other_variable = "value"
}

Then, in the /infra/App1 directory, run "terraform get", and "terraform apply".  This will create /infra/App1/terraform.tfstate.  Repeat for App2.

This does assume the two deployments are totally separate.  If there's anything shared, from what I can tell you need remote state and a multiple-layer deployment to manage it.

Amit Chauhan

unread,
Nov 17, 2015, 8:54:48 PM11/17/15
to terrafo...@googlegroups.com
Thanks David for your reply. However the problem is that this approach is fine if you already know about all the applications before hand.  Think of a Aws Cicd pipeline where any application could be one click deployable. All an application needs to do is to have a deployable config ( some custom format not TF) and it could be part of Cicd. How to handle such dynamic stack creation with Terraform?
--
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 a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/zWYn6wRhfgY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2f5d64eb-2b81-4082-a40a-cf06eb9d8b7c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages