Terraform use module output variables as inputs for another module?

One of the key features that sets Terraform apart is its modular architecture, enabling the reuse of code and fostering collaboration across teams. In this blog post, we'll dive deep into the concept of output variables in Terraform modules and explore how they can be leveraged as inputs for another module, enabling powerful infrastructure orchestration and flexibility.

Table of content

  1. Understanding Terraform Modules
  2. Exploring Output Variables
  3. Defining Output Variables
  4. Using Output Variables as Inputs
  5. Benefits and Use Cases
  6. Conclusion

1. Understanding Terraform Modules:

Before we delve into the specifics of output variables, it's essential to grasp the fundamentals of Terraform modules. Modules serve as self-contained, reusable blocks of Terraform configuration that encapsulate a specific set of resources and their dependencies. They promote code reuse, modularity, and maintainability, making it easier to manage and scale infrastructure deployments.

2. Exploring Output Variables:

Output variables in Terraform allow us to export specific values from a module and make them available for other modules or the root module. These outputs act as the interface between different modules, facilitating the passing of information, such as resource attributes, IP addresses, or configuration data, from one module to another.

3. Defining Output Variables:

To define an output variable within a Terraform module, we utilize the output block. Let's consider an example where we have a module called "ec2_module" responsible for provisioning an EC2 Instance in AWS . Within the "ec2_module" module, we can define an output variable, such as "instance_id", which holds the ID of the created EC2 Instance:

 1# File path = ../ec2_module/main.tf
 3# Create an EC2 Instance 
 4resource "aws_instance" "example" {
 5  ami           = "ami-0c94855ba95c574c8"
 6  instance_type = "t2.micro"
 9# Set the output (this output will be used as a reference as input later from another module)
10output "instance_id" {
11  value = aws_instance.example.id

Here, aws_instance.example.id represents the resource ID of the EC2 Instance provisioned by the module.

4. Using Output Variables as Inputs:

Once we have defined the output variable in the source module, we can easily consume it as an input in another module.

Let's assume we have a separate module called "compute" responsible for provisioning virtual machines within the network created by the "network" module. We can use the output variable network_id from the "network" module as an input for the "compute" module:

 2## Call the EC2 Module  "../ec2_module"
 3module "ec2_module" {
 4  source = "../ec2_module"
 7# Using the put from the module "ec2_module" as input to the next module "compute"
 8module "compute" {
 9  source = "./modules/compute"
10  another_module_instance_id = module.ec2_module.instance_id

In this example, we reference the instance_id output variable from the "ec2_module" module using the module.ec2_module.instance_id syntax and pass it as an input to the "compute" module using the another_module_instance_id argument.

5. Benefits and Use Cases:

Utilizing output variables as inputs for another module brings several benefits and opens up a wide range of use cases:

  1. Dependency Management: By explicitly defining and consuming output variables, Terraform automatically manages the dependency graph between modules. This ensures that the dependent module waits for the required outputs before execution, promoting a consistent and reliable infrastructure deployment process.

  2. Modularity and Reusability: Output variables enable modular design, allowing teams to build and share Terraform modules with well-defined interfaces. Different teams can develop specialized modules, such as networking, storage, or security, and easily integrate them into broader infrastructure compositions.

  3. Dynamic Configuration: With output variables, modules can provide dynamic configuration data to other modules. For instance, the "network" module might export the subnet CIDR block, which can be consumed by the "compute" module to configure the network interfaces of the virtual machines.

6. Conclusion:

Leveraging output variables as inputs for other modules in Terraform unlocks powerful capabilities for infrastructure orchestration, collaboration, and reusability.

By effectively defining and consuming output variables, we can establish clear interfaces between modules, simplify dependency management, and build scalable and flexible infrastructure configurations. Embrace the modular nature of Terraform and explore the vast possibilities offered by output variables to streamline your infrastructure provisioning workflows.

Posts in this Series