To get the output from one module to another module in Terraform, you can use the following steps:
- Define the output in the first module.
- Import the second module.
- Reference the output of the first module in the second module.
Here is an example of how to do this:
# First module
module "web_server" {
source = "./web_server"
output "instance_ip_addr" {
value = aws_instance.web_server.public_ip
}
}
# Second module
module "database" {
source = "./database"
instance_ip_addr = module.web_server.instance_ip_addr
}Code language: PHP (php)
In this example, the web_server module defines an output called instance_ip_addr. The database module imports the web_server module and then references the instance_ip_addr output in its own configuration.
When you run terraform apply, Terraform will create the resources defined in both modules. The database module will use the instance_ip_addr output from the web_server module to connect to the web server.
Here is an explanation of the code:
- The
sourceattribute in themoduleblock specifies the location of the module definition. In this case, the module definition is located in theweb_serverdirectory. - The
outputblock defines an output calledinstance_ip_addr. The value of the output is the public IP address of the web server instance. - The
sourceattribute in themoduleblock specifies the location of the module definition. In this case, the module definition is located in thedatabasedirectory. - The
instance_ip_addrattribute references theinstance_ip_addroutput from theweb_servermodule.
You can use outputs to accomplish this. In Terraform, a module can also return values. Again, this is done using a mechanism you already know: output variables. Such as if there are 2 modules, kafka and cloudwatch.
In your kafka module, you could define an output that looks something like this:
output "instance_ids" {
value = ["${aws_instance.kafka.*.id}"]
}Code language: JavaScript (javascript)
Case 1 – If you are calling kafka module from cloudwatch module by instantiated the module with something like:
module "kafka" {
source = "./modules/kafka"
}
# You can then access that output as follows:
instances = ["${module.kafka.instance_ids}"]Code language: PHP (php)
Case 2 – If your modules are isolated from each other (i.e. your cloudwatch module doesn’t instantiate your kafka module), you can pass the outputs as variables between modules:
module "kafka" {
source = "./modules/kafka"
}
module "cloudwatch" {
source = "./modules/cloudwatch"
instances = ["${module.kafka.instance_ids}"]
}
# Of course, your "cloudwatch" module would have to declare the instances variable.Code language: PHP (php)
Case 3 – You can add the ASG name as an output variable in /modules/services/webserver-cluster/outputs.tf as follows.
output "asg_name" {
value = aws_autoscaling_group.example.name
description = "The name of the Auto Scaling Group"
}
You can access module output variables the same way as resource output attributes. The syntax is:
module.<MODULE_NAME>.<OUTPUT_NAME>
module.frontend.asg_nameCode language: JavaScript (javascript)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals