An output
in Terraform is a way to view the result of an action that has been performed, or resource that has been created.
Let’s say that you have some code to create a Load Balancer, or Assign a Public IP. Once this action is complete, the output
block will have access to this created value.
An simple output
example
outputs.tf
output "instance_ips" {
value = aws_instance.web.*.public_ip
}
output "lb_address" {
value = aws_alb.web.public_dns
}
This will output the values to the console once the terraform apply
step has successfully run.
But what happens if you have some outputs in a module that your own Terraform calls?
Outputs are only rendered to the immediate caller, and not traversed up the call tree to the initiation main.tf
.
An example project structure
./
./modules/
./example_mod
outputs.tf
resources.tf
./main.tf
In our main.tf
we call:
module "example_mod" {
source = "./modules/example_mod"
}
This will not render any output
to the console, as the output.tf
is segmented to the module itself and not passed up the chain.
To get access to these outputs after module creation, we need to do the following:
How to pass a Module’s Output up the tree to the Parent
./
./modules/
./example_mod
outputs.tf
resources.tf
./main.tf
./output.tf # <- add this
In our output.tf
file:
output "THE_INSTANCE_IPS" {
value = module.example_mod.instance_ips
}
output "THE_LB_ADDRESS" {
value = module.example_mod.lb_address
}