0
0
Terraformcloud~5 mins

Outputs for module communication in Terraform - Commands & Configuration

Choose your learning style9 modes available
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.