Bird
Raised Fist0
Terraformcloud~10 mins

Outputs for module communication in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Process Flow - Outputs for module communication
Define output in child module
↓
Apply child module
↓
Output value stored
↓
Parent module calls child module
↓
Parent accesses output via module.<name>.<output>
↓
Use output in parent resources or variables
Outputs in Terraform modules let child modules share values with parent modules, enabling communication and reuse.
Execution Sample
Terraform
module "child" {
  source = "./child"
}

output "child_value" {
  value = module.child.example_output
}
Parent module calls child module and accesses its output named example_output.
Process Table
StepActionEvaluationResult
1Child module defines output 'example_output'Set output value to 'Hello from child'Output stored in child module state
2Parent module calls child moduleChild module initialized and appliedChild module resources created
3Parent accesses module.child.example_outputRetrieve output value'Hello from child' returned
4Parent uses output in its own output 'child_value'Assign value'child_value' output set to 'Hello from child'
5Terraform apply completesOutputs availableParent output 'child_value' shows 'Hello from child'
💡 All outputs resolved and available for parent module use after apply
Status Tracker
VariableStartAfter Step 1After Step 3Final
example_outputundefined'Hello from child''Hello from child''Hello from child'
child_valueundefinedundefinedundefined'Hello from child'
Key Moments - 2 Insights
Why can't the parent module access child module variables directly?
The parent module accesses values only through outputs defined in the child module, as shown in step 3 of the execution_table.
What happens if the child module does not define an output?
The parent module cannot retrieve any value from the child module, so referencing module.child.<output> will fail, as outputs must be explicitly declared (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'example_output' after step 1?
Aundefined
B'Hello from child'
Cnull
DError
💡 Hint
Check the 'Result' column in row for step 1 in execution_table
At which step does the parent module retrieve the child's output value?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'Parent accesses module.child.example_output' in execution_table
If the child module output value changed, which step would reflect the new value?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Output value is set in child module definition at step 1
Concept Snapshot
Terraform module outputs:
- Defined in child module with 'output' block
- Provide values to parent module
- Accessed via module.<name>.<output>
- Enable communication between modules
- Must be declared explicitly to be accessible
Full Transcript
In Terraform, outputs allow a child module to share values with its parent module. The child module defines outputs using the output block. When the parent module calls the child module, it can access these outputs using the syntax module.<child_name>.<output_name>. This process happens during terraform apply, where the child module runs, sets its outputs, and the parent module retrieves them. Outputs enable modules to communicate and reuse values safely and clearly.

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

  1. 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.
  2. Step 2: Differentiate outputs from other blocks

    Unlike resource or variable blocks, outputs do not create or accept input but share information.
  3. Final Answer:

    To share values from one module to another or to the user -> Option A
  4. 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

  1. Step 1: Recall Terraform output block syntax

    Outputs use the syntax: output "name" { value = expression }
  2. 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.
  3. Final Answer:

    output "instance_ip" { value = aws_instance.web.private_ip } -> Option B
  4. 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
}

And this root module code:
module "database" {
  source = "./modules/db"
}

output "database_url" {
  value = module.database.db_endpoint
}

What will terraform output database_url show after deployment?
medium
A. The endpoint address of the AWS database instance
B. The string 'module.database.db_endpoint'
C. An error because outputs cannot be nested
D. The IP address of the AWS database instance

Solution

  1. Step 1: Understand module output forwarding

    The module outputs 'db_endpoint' which is accessed in root as 'module.database.db_endpoint'.
  2. Step 2: Check root output value

    The root output 'database_url' uses the module output value, so it will show the actual endpoint string.
  3. Final Answer:

    The endpoint address of the AWS database instance -> Option A
  4. Quick Check:

    Module output forwarded = A [OK]
Hint: Root output uses module output value directly [OK]
Common Mistakes:
  • Thinking output shows variable name as string
  • Assuming outputs cannot be nested
  • Confusing endpoint with IP address
4. You wrote this output block inside a module:
output "server_ip" {
  value = aws_instance.app.private_ip
}

But when you run terraform apply, you get an error: Reference to undeclared resource. What is the likely cause?
medium
A. The output block is missing a description
B. Output blocks cannot reference resource attributes
C. Output names cannot contain underscores
D. The resource aws_instance.app is not defined in the module

Solution

  1. Step 1: Analyze the error message

    'Reference to undeclared resource' means Terraform cannot find 'aws_instance.app' in the module.
  2. Step 2: Check output references

    Outputs must reference existing resources; if resource is missing, error occurs.
  3. Final Answer:

    The resource aws_instance.app is not defined in the module -> Option D
  4. Quick Check:

    Missing resource causes reference error = A [OK]
Hint: Check resource exists before referencing in output [OK]
Common Mistakes:
  • Assuming outputs can't reference resources
  • Thinking underscores are invalid in output names
  • Believing description is mandatory
5. You have a module that creates multiple AWS instances and outputs a list of their private IPs:
output "instance_ips" {
  value = [for i in aws_instance.app : i.private_ip]
}

How can you use this output in the root module to create a security group rule allowing SSH from all these IPs?
hard
A. Use count = length(module.app.instance_ips) but no for_each
B. Directly assign cidr_blocks = module.app.instance_ips without conversion
C. Use for_each = toset(module.app.instance_ips) in the security group rule resource
D. Outputs cannot be used to create security group rules

Solution

  1. Step 1: Understand output is a list of IPs

    The output returns a list of private IP addresses from multiple instances.
  2. Step 2: Use for_each with a set for unique IPs

    Security group rules can be created per IP using for_each with toset() to avoid duplicates.
  3. Final Answer:

    Use for_each = toset(module.app.instance_ips) in the security group rule resource -> Option C
  4. Quick Check:

    Use for_each with toset for multiple IP rules = C [OK]
Hint: Use for_each with toset() for list outputs in resources [OK]
Common Mistakes:
  • Assigning list directly to cidr_blocks without for_each
  • Using count without mapping IPs
  • Thinking outputs can't be used in resource blocks