Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Use a variable block with the name network_id and set type = string.
Practice
(1/5)
1. What is the main purpose of output blocks in Terraform modules?
easy
A. To share values from one module to another or to the user
B. To create new resources in the cloud
C. To define variables for user input
D. To delete resources after deployment
Solution
Step 1: Understand the role of output blocks
Output blocks are used to expose values from a module so other modules or users can access them.
Step 2: Differentiate outputs from other blocks
Unlike resource or variable blocks, outputs do not create or accept input but share information.
Final Answer:
To share values from one module to another or to the user -> Option A
Quick Check:
Outputs share values = D [OK]
Hint: Outputs share info between modules or users [OK]
Common Mistakes:
Confusing outputs with resource creation
Thinking outputs accept user input
Assuming outputs delete resources
2. Which of the following is the correct syntax to define an output named instance_ip with value aws_instance.web.private_ip?
easy
A. output instance_ip = aws_instance.web.private_ip
B. output "instance_ip" { value = aws_instance.web.private_ip }
C. output "instance_ip" = aws_instance.web.private_ip
D. output { instance_ip = aws_instance.web.private_ip }
Solution
Step 1: Recall Terraform output block syntax
Outputs use the syntax: output "name" { value = expression }
Step 2: Match syntax with options
output "instance_ip" { value = aws_instance.web.private_ip } matches the correct syntax with quotes and value assignment inside braces.
Final Answer:
output "instance_ip" { value = aws_instance.web.private_ip } -> Option B
Quick Check:
Correct output block syntax = B [OK]
Hint: Output blocks need quotes and value inside braces [OK]
Common Mistakes:
Missing quotes around output name
Using equals sign outside braces
Placing value outside output block
3. Given this module output block:
output "db_endpoint" {
value = aws_db_instance.main.endpoint
}