0
0
Terraformcloud~5 mins

Module outputs in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Terraform modules, you often want to get information from them after they run. Module outputs let you share useful values from a module to the place where you use it. This helps you connect parts of your infrastructure easily.
When you want to get the IP address of a server created inside a module to use it elsewhere.
When you need to pass the ID of a created database from a module to another resource.
When you want to see the URL of a load balancer created inside a module after deployment.
When you want to share a security group ID created in a module with other modules or resources.
When you want to output the name of a storage bucket created inside a module for reference.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

module "my_app" {
  source = "./modules/app_server"
  server_name = "example-server"
}

output "server_ip" {
  value       = module.my_app.server_ip
  description = "The IP address of the app server"
}

This file uses a module called my_app from a local folder ./modules/app_server. It passes a server name to the module.

The output block named server_ip shows how to get the IP address value from the module and make it visible after Terraform runs.

Commands
This command sets up Terraform in the current folder. It downloads the module code and prepares everything needed to run Terraform.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... 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 creates or updates the infrastructure as defined in the Terraform files. The -auto-approve flag skips the confirmation step to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
module.my_app.aws_instance.app_server: Creating... module.my_app.aws_instance.app_server: Creation complete after 10s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: server_ip = "54.123.45.67"
-auto-approve - Skip manual approval to apply changes immediately
This command shows the value of the output named server_ip. It helps you quickly see the IP address without reading the whole Terraform state.
Terminal
terraform output server_ip
Expected OutputExpected
54.123.45.67
Key Concept

If you remember nothing else from this pattern, remember: module outputs let you share important values from inside a module to the outside world for easy use.

Common Mistakes
Trying to access a module output without defining it inside the module.
Terraform will not know what value to show and will give an error or empty output.
Always define an output block inside the module to expose the value you want to share.
Using the wrong syntax to access module outputs, like missing the 'module.' prefix.
Terraform requires the 'module.' prefix to access outputs from modules, otherwise it treats it as a local variable and fails.
Use 'module.module_name.output_name' to correctly reference outputs from modules.
Summary
Use 'output' blocks inside modules to expose values you want to share.
Access module outputs with 'module.module_name.output_name' in your root Terraform files.
Run 'terraform output output_name' to see the value after applying changes.