Bird
Raised Fist0
Terraformcloud~5 mins

Outputs for module communication in Terraform - Commands & Configuration

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
Introduction
When you build infrastructure with Terraform, you often split your setup into smaller parts called modules. Outputs let these modules share important information with each other, like IP addresses or resource IDs, so they can work together smoothly.
When you want one module to provide a value that another module needs to use.
When you need to see important information from a module after Terraform finishes applying changes.
When you want to organize your infrastructure into reusable parts that communicate cleanly.
When you want to avoid repeating the same resource definitions by sharing outputs.
When you want to pass dynamic values from one module to another without hardcoding.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

module "network" {
  source = "./modules/network"
}

module "server" {
  source = "./modules/server"
  subnet_id = module.network.subnet_id
}

output "server_ip" {
  value = module.server.instance_ip
}

This main Terraform file uses two modules: network and server.

The network module creates a subnet and shares its ID as an output called subnet_id.

The server module receives the subnet_id as input to place a server in that subnet.

Finally, the main file outputs the server's IP address from the server module so you can see it after deployment.

Commands
This command sets up Terraform in the current folder, downloading any modules and providers needed before you can apply your configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration, creating or updating resources. The -auto-approve flag skips the manual confirmation step.
Terminal
terraform apply -auto-approve
Expected OutputExpected
module.network.aws_subnet.example: Creating... module.network.aws_subnet.example: Creation complete after 2s [id=subnet-12345678] module.server.aws_instance.example: Creating... module.server.aws_instance.example: Still creating... [10s elapsed] module.server.aws_instance.example: Creation complete after 15s [id=i-0abcdef1234567890] Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: server_ip = "10.0.1.5"
→
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the value of the output named server_ip, which is the IP address of the server created by the server module.
Terminal
terraform output server_ip
Expected OutputExpected
10.0.1.5
Key Concept

If you remember nothing else from this pattern, remember: outputs let one module share important values with another module or with the user after deployment.

Common Mistakes
Not defining outputs in the module to share values.
Without outputs, other modules or the main configuration cannot access the module's internal values.
Always define output blocks in your module for any value you want to share externally.
Trying to access a module's internal resource attribute directly without an output.
Terraform modules hide their internal resources; you must use outputs to expose values.
Create an output in the module that returns the needed attribute, then access it via module.<name>.<output>.
Forgetting to pass module outputs as inputs to other modules.
Modules cannot communicate unless you explicitly pass outputs as inputs, so resources won't connect properly.
Use the output from one module as an input variable value when calling another module.
Summary
Define outputs in modules to share important values like IDs or IPs.
Use module outputs as inputs to other modules to connect resources.
Use 'terraform output' to see module outputs after deployment.

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