Cross reference variable from another module

4,039 views
Skip to first unread message

Kenan Virtucio

unread,
Sep 13, 2017, 1:21:13 AM9/13/17
to Terraform
Hi, 

I have this directory structure:

|-- modules
|   |-- network
|   |   |-- main.tf
|   |   |-- outputs.tf
|   |   `-- vars.tf
|   `-- services
|       `-- frontend-app
|           |-- main.tf
|           |-- outputs.tf
|           `-- vars.tf
`-- sample_customer
    |-- network
    |   |-- main.tf
    |   `-- vars.tf
    |-- services
    |   `-- frontend-app
    |       |-- main.tf
    `       `-- vars.tf

I just a question regarding cross referencing variables:

Under modules -> network -> main.tf, I have this resource declared:

resource "aws_subnet" "subnet1" {
  cidr_block        = "${element(var.subnet, 0)}"
  vpc_id            = "${aws_vpc.example_vpc.id}"
  availability_zone = "${element(var.availability_zone, 0)}"
}

Under modules -> network -> output.tf, I have this declaration:

output "subnet1" {
}

I need to reference this variable subnet output under network module to be used in frontend-app module.

So here's how i cross reference it:

Under sample_customer -> services -> frontend-app -> main.tf

module "frontend" {
  source  = "../../../modules/services/frontend/" 
  subnet1 = "${module.network.subnet1}"
}

If i do this, i get an error: * module 'frontend': unknown module referenced: network
So my question is, how can i reference a variable from another module? It's really causing me confusion.

Thanks!

Lowe Schmidt

unread,
Sep 13, 2017, 3:51:02 AM9/13/17
to terrafo...@googlegroups.com
How and where do you instantiate the network module? 

--
Lowe Schmidt | +46 723 867 157

--
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-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/4d20b480-7aa4-4d22-b6df-319d5ec08aa0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kenan Virtucio

unread,
Sep 13, 2017, 4:34:14 AM9/13/17
to Terraform
Actually that is my question. How should instantiate it? Because it's really throwing me off.

Here, let me explain.

The whole content of sample_customer -> services -> frontend-app -> main.tf

module "frontend" {
  source  = "../../../modules/services/frontend/" 
  subnet1 = "${module.network.subnet1}"
}

According to the documentation of terraform, when i declare the variable subnet1 i have first to declare/instantiate the module first. So what would happen to the main.tf is:
module "network" {
  source = "../modules/network/"
}

module "frontend" {
  source  = "../../../modules/services/frontend/" 
  subnet1 = "${module.network.subnet1}"
}

This new declaration would present new problems:

1) Whenever i declare the module network (i'm assuming they'll become sibling modules), it will re-instantiate the entire network and i don't want that to happen.
2) I said in #1 that the VPC was already instantiated because of sample_customer -> network -> main.tf:

module "network" {
  source = "../modules/network/"
}

Here's my use case: I want to re-use my entire infrastructure, this includes the VPC as well. Also, the newly spawned web apps should be within this VPC network.
The only way i do is i have to add the option vpc_zone_identifier: https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html. So i have to cross reference the newly spawned VPC resource (using output of terraform) to  another module.

I got this idea from: 
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Lowe Schmidt

unread,
Sep 13, 2017, 5:22:54 AM9/13/17
to terrafo...@googlegroups.com
Right, so you have one VPC + network subnets and multiple customer modules instantiated correct?

So, to share the network part without instantiating the VPC/Network modules on every customer you will either have to use the remote state data source https://www.terraform.io/docs/providers/terraform/d/remote_state.html or the aws_subnet_id data source https://www.terraform.io/docs/providers/aws/d/subnet_ids.html 

--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/b40f9af3-ee56-4413-8bc0-8dace2ae9627%40googlegroups.com.

Kenan Virtucio

unread,
Sep 13, 2017, 5:30:21 AM9/13/17
to Terraform
Ideally, my goal is that for each customer thy will have their own VPC (including their own subnets) and all their corresponding modules will be within their own VPC.

Kenan Virtucio

unread,
Sep 21, 2017, 2:10:29 AM9/21/17
to Terraform
Thanks Lowe for the suggestion. I finally made it work.

Reply all
Reply to author
Forward
0 new messages