🐶
Terraform

Terraform Output Not Working in Module: Troubleshooting Tips

By Ondřej Dolanský on 12/19/2024

Learn why your Terraform outputs might not be working as expected within modules and how to troubleshoot common issues.

Terraform Output Not Working in Module: Troubleshooting Tips

Table of Contents

Introduction

To access outputs from Terraform modules, you need to explicitly export them from the module and then reference them in your root module. This involves a two-step process: exporting outputs within the module and referencing them in the root module.

Step-by-Step Guide

To access outputs from Terraform modules, you need to explicitly export them from the module and then reference them in your root module.

1. Export Outputs within the Module:

Inside your module's output.tf file, define the outputs you want to make available:

output "example_output" {
  value = aws_instance.example.id
}

2. Reference Outputs in the Root Module:

In your root module where you call the child module, access the output values using the module name and output name:

module "my_module" {
  source = "./modules/my-module"
}

output "module_output" {
  value = module.my_module.example_output
}

This structure ensures that outputs from child modules are explicitly passed to the root module, making them accessible for use in other parts of your Terraform code.

Code Example

The code defines a Terraform module that provisions an AWS instance and exports its ID and public IP address as outputs. The root module then consumes this child module and accesses its outputs to display the instance information. This showcases how to create reusable infrastructure components and share data between modules in Terraform.

This example demonstrates how to export outputs from a child module and access them in the root module.

File Structure:

├── modules
│   └── my-module
│       └── main.tf
└── main.tf

modules/my-module/main.tf:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159c9e670235" # Replace with your desired AMI
  instance_type = "t2.micro"
  tags = {
    Name = "Example Instance from Module"
  }
}

output "instance_id" {
  value = aws_instance.example.id
}

output "instance_public_ip" {
  value = aws_instance.example.public_ip
}

main.tf:

module "my_module" {
  source = "./modules/my-module"
}

output "module_instance_id" {
  value = module.my_module.instance_id
}

output "module_instance_public_ip" {
  value = module.my_module.instance_public_ip
}

Explanation:

  1. Child Module (modules/my-module/main.tf):

    • We define an AWS instance resource.
    • We define two outputs: instance_id and instance_public_ip, which expose the instance ID and public IP address respectively.
  2. Root Module (main.tf):

    • We declare a module named my_module and specify its source directory.
    • We define two outputs: module_instance_id and module_instance_public_ip. These outputs retrieve the values from the corresponding outputs defined in the child module using the syntax module.<module_name>.<output_name>.

By running terraform apply, you can create the infrastructure and see the output values, including the instance ID and public IP address, which are accessible through the root module. This demonstrates how to effectively export and utilize outputs from Terraform modules.

Additional Notes

Importance of Explicit Outputs:

  • Encapsulation: Explicit outputs enforce a clear contract between modules, hiding internal implementation details and exposing only what's necessary.
  • Reusability: Well-defined outputs make modules more reusable across different projects and teams.
  • Maintainability: Explicit outputs make it easier to understand and modify Terraform code without unintended side effects.

Best Practices:

  • Descriptive Naming: Use clear and concise names for outputs that reflect their purpose.
  • Data Types: Specify the expected data type for each output (e.g., string, list, map) to improve code clarity and error detection.
  • Documentation: Add comments to your output definitions to explain their purpose and usage.

Common Use Cases:

  • Passing Resource IDs: Outputs are frequently used to share resource identifiers (e.g., instance IDs, security group IDs) between modules.
  • Exposing Configuration Values: Outputs can expose calculated values or configuration settings from a module to be used by other modules or scripts.
  • Creating Dependencies: Outputs can be used to establish implicit dependencies between resources in different modules.

Troubleshooting:

  • Output Not Found: Double-check the output name and module name for typos. Ensure the output is defined within the module's output.tf file.
  • Empty Output: Verify that the resource or value being referenced in the output is actually created or available.
  • Circular Dependencies: Avoid creating circular dependencies between modules, where module A depends on an output from module B, which in turn depends on an output from module A.

Beyond the Basics:

  • Output Values in Remote State: When using remote state, outputs are stored in the remote backend and can be accessed by other workspaces or teams.
  • Terraform Output Command: The terraform output command can be used to retrieve and display output values after a successful terraform apply.
  • Data Sources: Outputs can be used as inputs to data sources, allowing you to query information based on resources created by modules.

Summary

Step Description Example
1. Export Outputs within the Module Define outputs in the module's output.tf file to make them accessible. terraform output "example_output" { value = aws_instance.example.id }
2. Reference Outputs in the Root Module Access output values in your root module using the module name and output name. terraform module "my_module" { source = "./modules/my-module" } output "module_output" { value = module.my_module.example_output }

Summary:

To use outputs from Terraform modules, you must explicitly export them from the module's output.tf file and then reference them in your root module using the module name and output name. This ensures that outputs are passed from child modules to the root module for use in other parts of your Terraform code.

Conclusion

By following these practices, you can leverage Terraform modules to create well-organized, reusable, and maintainable infrastructure code. Understanding how to define and access outputs is crucial for building complex and interconnected Terraform projects.

References

  • Output is not published by a root module - HCP Terraform ... Output is not published by a root module - HCP Terraform ... | Hello, it appears that in order to obtain an “output” value from the root module, I have to define it (output) twice. Once in the root and then in the calling module. Is this an expected behavior and why? I’d think that publishing in the root level should be sufficient… main.tf: module "vm_key" { source = "../../../modules/AWS/comp/keys" ... } output "vm_key" { value = module.vm_key.key_name } root module ( …/…/…/modules/AWS/comp/keys/keys.tf) : resource "aws_key_pair" "vm_key" { .....
  • terraform output not display the outputs · Issue #35105 · hashicorp ... terraform output not display the outputs · Issue #35105 · hashicorp ... | Terraform Version Terraform v1.8.2 on linux_amd64 + provider registry.terraform.io/cloudflare/cloudflare v4.30.0 + provider registry.terraform.io/hashicorp/aws v5.47.0 + provider registry.terraform...
  • terraform - Outputs not displaying when using modules - Stack ... terraform - Outputs not displaying when using modules - Stack ... | Jul 20, 2019 ... Only the outputs of the root module are captured and displayed by Terraform. If you need to pass outputs from a module to the root module ...
  • Terraform output id is not returning the values terraform 0.14.5 vs ... Terraform output id is not returning the values terraform 0.14.5 vs ... | Hi I see the difference in terraform output command in terraform 0.14.5 vs 0.12.26 .Below is the code Version.tf terraform { required_version = "= 0.14.5" required_providers { aws = { source = "repo.dtcc.com/hashicorp/aws" version = "4.13.0" } null = { source = "repo.dtcc.com/hashicorp/null" version = "3.1.1" } template = { source = "repo.dtcc.com/hashicorp/template" version = "2.2.0" } } } Bellow is the output.tf output...
  • Terraform Console Unable to Output Module Outputs · Issue #21516 ... Terraform Console Unable to Output Module Outputs · Issue #21516 ... | After upgrading to Terraform v0.12, it appears Terraform console no longer is able to access module outputs from a remote state (or state in general). It always seems to expect a terraform apply to...
  • Terragrunt dependencies/outputs problem - Gruntwork Customers ... Terragrunt dependencies/outputs problem - Gruntwork Customers ... | Hi everyone! I having a problem working with Terragrunt and module dependencies. I have a Kinesis Stream, a Lambda function consuming and processing events from Kinesis, and a SQS queue as a dead-letter queue if something went wrong when the Lambda function process an event. Terraform module file Using the following Terraform module, I create an event-mapping between the Kinesis Stream and the Lambda function. resource "aws_lambda_event_source_mapping" "kinesis_lambda_event_mapping" { /...
  • Terraform 0.12.5 cannot output module values · Issue #22126 ... Terraform 0.12.5 cannot output module values · Issue #22126 ... | Terraform Version v0.12.5 Terraform Configuration Files output.tf in my module output "image" { value = var.image } main.tf in my root module module "dns_override" { source = "./mymodule" } Expecte...
  • Best practice for module outputs? : r/Terraform Best practice for module outputs? : r/Terraform | Posted by u/WolfPusssy - 9 votes and 7 comments
  • Output does not not show module outputs · Issue #1940 ... Output does not not show module outputs · Issue #1940 ... | Hey all, I've got a state that looks something like this - just a single module with all my resources and an output defined within: terraform.tfstate: { "version": 1, "serial": 32, "modules": [ { "...

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait