MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to use one module’s variable in another module in terraform?


To get the output from one module to another module in Terraform, you can use the following steps:

  1. Define the output in the first module.
  2. Import the second module.
  3. 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 source attribute in the module block specifies the location of the module definition. In this case, the module definition is located in the web_server directory.
  • The output block defines an output called instance_ip_addr. The value of the output is the public IP address of the web server instance.
  • The source attribute in the module block specifies the location of the module definition. In this case, the module definition is located in the database directory.
  • The instance_ip_addr attribute references the instance_ip_addr output from the web_server module.

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)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
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.

Related Posts

Terraform: Azure VM using terraform-azurerm-compute Module

The example above uses the community Azure/compute/azurerm and Azure/network/azurerm Terraform modules to build one Linux VM and one Windows VM in Azure. Both share a virtual network…

Read More

Terraform: Azure VM using virtual-machine Module

This tutorial uses the community kumarvna/virtual-machine/azurerm module from the Terraform Registry to deploy two Windows Server 2019 VMs into an existing Azure resource group, virtual network and…

Read More

Terraform Tools for Developer Productivity Master Guide

Tools, Workflow, Security, Testing, CI/CD, Automation, AI Assistance, Governance, and Production Best Practices Verified baseline: 5 September 2026Audience: Terraform developers, DevOps/SRE, platform/cloud engineers, module authors, DevSecOps teams, and engineering…

Read More

AWS EKS: How to enable Auto Mode using Terraform?

Let’s clarify the reality for July 2026: EKS “Auto Mode” vs. “Karpenter Auto Compute” in Terraform 1. The Real EKS Auto Mode (Serverless) 2. cluster_compute_config in terraform-aws-modules/eks…

Read More

Kubernetes: Deploying AWS Load Balancer Controller with Terraform

Absolutely, deploying the AWS Load Balancer Controller (ALB Controller) in production via Terraform is now a very common, robust, and recommended practice. There are official Terraform modules…

Read More

Terraform source for Modules from Git: All Patterns & Examples

Here’s a comprehensive tutorial on all valid Terraform module “source” Git URL patterns, with clear examples (no Drivemode references), so you can master using modules from GitHub,…

Read More