1

I have following structure:

modules
 |_ test1
 |    |_vpc.tf
 |_test2
      |_subnet.tf

I have created a vpc in test1/vpc.tf

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

I am getting vpc id in output like:

output "vpc_id" {
  value = aws_vpc.main.id
}

How can I pass this id to test2/subnet.tf file? I am searching online and can't seem to find an answer for this.

NoviceMe
  • 3,126
  • 11
  • 57
  • 117

1 Answers1

7

Create a variable in subnet.tf:

variable "vpc_id" {
  type = string
}

Then in your main terraform file where you are utilizing both of these modules, you would take the output from the vpc module and pass it to the input of the subnet module:

module "vpc" {
  source = "modules/test1"
}

module "subnet" {
  source = "modules/test2"
  vpc_id = module.vpc.vpc_id
}
Mark B
  • 183,023
  • 24
  • 297
  • 295