0
0
Terraformcloud~30 mins

Outputs for module communication in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Outputs for module communication
📖 Scenario: You are building a Terraform project with two modules: network and server. The network module creates a virtual network and outputs its ID. The server module needs to use this network ID to launch a server inside the network.
🎯 Goal: Create Terraform outputs in the network module to share the network ID. Then, in the root module, capture this output and pass it as an input variable to the server module.
📋 What You'll Learn
Create a Terraform output called vnet_id in the network module that outputs the virtual network ID.
In the root module, declare the network module and capture its vnet_id output.
Declare the server module in the root module and pass the captured vnet_id as an input variable called network_id.
In the server module, declare an input variable called network_id.
💡 Why This Matters
🌍 Real World
In real cloud projects, modules often need to share information like network IDs, security group IDs, or subnet IDs. Outputs and inputs help modules communicate cleanly.
💼 Career
Understanding module communication is essential for building scalable and maintainable Terraform infrastructure code, a key skill for cloud engineers and DevOps professionals.
Progress0 / 4 steps
1
Create the network module output
In the network module, create an output called vnet_id that outputs the value of the resource azurerm_virtual_network.main.id.
Terraform
Need a hint?

Use the output block with the name vnet_id and set its value to azurerm_virtual_network.main.id.

2
Declare the network module in the root module
In the root module, declare a module called network with the source path ./modules/network. Capture its output vnet_id by referencing module.network.vnet_id in a locals block as network_id.
Terraform
Need a hint?

Declare the network module with source = "./modules/network". Then create a locals block with network_id = module.network.vnet_id.

3
Declare the server module and pass network_id
In the root module, declare a module called server with the source path ./modules/server. Pass the captured network_id local value as an input variable called network_id to the server module.
Terraform
Need a hint?

Declare the server module with source = "./modules/server" and pass network_id = local.network_id.

4
Declare the input variable network_id in the server module
In the server module, declare an input variable called network_id of type string.
Terraform
Need a hint?

Use a variable block with the name network_id and set type = string.