how to include different main.tf files in different directories to one environment ?

5,999 views
Skip to first unread message

kiran sangeetam

unread,
Dec 12, 2017, 5:59:08 AM12/12/17
to Terraform
Hi Everyone,

I have multiple directories with respective .tf files like below.

├── jenkins
│   ├── main.tf
│   ├── terraform.tfvars
│   └── vars.tf
├── mirth
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfvars
│   └── vars.tf
├── mongodb
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfvars
│   └── vars.tf

All main.tf files are using their respective modules. My requirement is to add those servers (or call files in those directories) in one main file, let's say 'staging/main.tf' and run only that one file for all those servers. I couldn't find how to do that. Could someone please help on how can I achieve this ?

Thanks
Kiran

Lowe Schmidt

unread,
Dec 12, 2017, 6:31:22 AM12/12/17
to terrafo...@googlegroups.com
You can just symlink all the files into a new directory, or cat the files into a new one.
> --
> 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/58242234-bb8b-4185-88e6-6734cedff438%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

kiran sangeetam

unread,
Dec 12, 2017, 7:22:31 AM12/12/17
to Terraform
Hi Lowe,

Thanks for the quick reply. I have one question. If I symlink all those files into new directory, var.tf file in difefrent directories has different values for same variable "count". So how can I resolve this for variables with the same name ? Because if I need to rename variables with the same name it'll be lengthy process for me to find and rename all of them.

Sean Johnson

unread,
Dec 12, 2017, 10:42:55 AM12/12/17
to terrafo...@googlegroups.com
I haven’t tried this (and it may be a bad idea for reasons I’m unaware of), but I wonder if you could reference each of those directories as modules in your “staging/main.tf”.

So, in staging/main.tf, there would be something like :

module “jenkins” {
  source = “../jenkins”
}

module “mirth” {
  source = “../mirth”
}

And so on. 

Just a thought. :) 

~Sean 

kiran sangeetam

unread,
Dec 12, 2017, 11:03:45 AM12/12/17
to Terraform
Hi Johnson,

Tried it.

module "ops" {
  source                        = "../resources/jenkins"
}

But it is throwing variable errors like below even though all are defined in '../resources/jenkins' terraform.tfvars file.

Error getting plugins: module root:
    module ops: required variable "jenkins_ami_id" not set
    module ops: required variable "key_name" not set

Sean Johnson

unread,
Dec 12, 2017, 3:26:54 PM12/12/17
to terrafo...@googlegroups.com
But it is throwing variable errors like below even though all are defined in '../resources/jenkins' terraform.tfvars file.

I think the tfvar file(s) would need to be in the “staging” directory in this context. Or, perhaps, you could pass in the tfvar file(s) as an option to the terraform call itself. 

~Sean


kiran sangeetam

unread,
Dec 13, 2017, 2:28:48 AM12/13/17
to Terraform
yeah, but to have all var files in "staging" folder, we have some variables with the same name (like "count=3" for instance count). And if I have to rename variables with the same name it'll be lengthy process for me to find and rename all of them.
Reply all
Reply to author
Forward
0 new messages