Search⌘ K

Module Outputs

Explore the use of outputs within Terraform modules and how they enable communication between modules. Discover the proper syntax for referencing variables from sourced modules and understand the importance of declaring outputs to share data safely and reliably across your Terraform configuration.

We’re familiar with the modules’ outputs, but in this lesson, we’ll look at the more nuanced use of outputs and their visibility to other modules.

Terraform outputs and module relationships become more important as our scripts become increasingly sophisticated.

A simple output module

While module inputs are defined with the variable keyword, outputs are defined with the output keyword. We’re going to type in another local module that outputs the filename of a local_file resource like this:

variable filename { 
    default = "default_filename.txt" 
} 
variable content { 
    default = "Hello terraform local output module!" 
} 
output filename { 
    value = var.filename 
} 
resource "local_file" "hello_local_output" { 
  content = var.content 
  filename = var.filename 
} 
The ltthw_output module structure

Now we type in the consumer module, output_module_consumer, that sources the output_module.

What do we expect to happen when we apply this module:

variable filename { 
    default = "default_filename.txt" 
} 
variable content { 
    default = "Hello terraform local output module!" 
} 
output filename { 
    value = var.filename 
} 
resource "local_file" "hello_local_output" { 
  content = var.content 
  filename = var.filename 
} 
output_module_consumer sources the "output_module

We can see that nothing was mentioned in any output and no filename was reported, so ...