3

Is there a way of using output values of a module that is located in another folder? Imagine the following environment:

tm-project/
├── lambda
│   └── vpctm-manager.js
├── networking
│   ├── init.tf
│   ├── terraform.tfvars
│   ├── variables.tf
│   └── vpc-tst.tf
├── prd
│   ├── init.tf
│   ├── instances.tf
│   ├── terraform.tfvars
│   └── variables.tf
└── security
    └── init.tf

I want to create EC2 instances and place them in a subnet that is declared in networking folder. So, I was wondering if by any chance I could access the outputs of the module I used in networking/vpc-tst.tf as the inputs of my prd/instances.tf.

Thanks in advances.

1 Answers1

4

You can use a outputs.tf file to define the outputs of a terraform module. Your output will have the variables name such as the content below.

output "vpc_id" {
  value = "${aws_vpc.default.id}"
}

These can then be referenced within your prd/instances.tf by referencing the resource name combined with the output name you defined in your file.

For example if you have a module named vpc which uses this module you could then use the output similar to below.

module "vpc" {
   ......
}

resource "aws_security_group" "my_sg" {
    vpc_id = module.vpc.vpc_id
}
Chris Williams
  • 32,215
  • 4
  • 30
  • 68