Outputs and module interfaces
Expose a useful result to a caller
An output is a named result of a Terraform module. For a root module it can serve as a value for a person or an automation script; for a child module it is part of the interface available to the calling module. Think of a reusable naming component that returns a name but keeps its naming formula private. This two-file example is complete when placed in the specified directories.
modules/naming/main.tf (child):
variable "label" { type = string}
locals { display_name = "app-${var.label}"}
output "name" { description = "Public name of the simulated application." value = local.display_name}main.tf (root):
module "naming" { source = "./modules/naming" label = "catalog"}
output "application_name" { description = "Name returned by the naming module." value = module.naming.name}The root passes catalog to the child input label. The child computes app-catalog and exposes it as name. The caller reads module.naming.name and returns it as application_name. A module is a directory of Terraform configuration; the source points to the supplied local child. Initialization discovers the child before planning. No servers, external providers or downloads are needed for this fixture.
Define a stable interface
The output label identifies the result, value computes it, and description tells callers what it represents. A caller cannot access the child local directly with module.naming.display_name. Choose a few useful, predictable outputs instead of exporting every internal attribute. Changing an output name or changing a string into an object can break existing consumers. Preserve the interface or coordinate a migration. References in the output expression normally establish the required dependency. Explicit depends_on is for an additional real dependency that the expression does not communicate; it is not a replacement for meaningful references and does not prove service health.
Check a condition before exposing an output
This separate local configuration checks a port contract before exposing it. It is not a server and does not bind a socket.
variable "port" { type = number default = 8080}
output "service_port" { description = "Port selected for a fictional local service." value = var.port
precondition { condition = var.port >= 1024 && var.port <= 65535 && floor(var.port) == var.port error_message = "Choose a whole number between 1024 and 65535." }}The default 8080 should pass; 80 and 65536 fail the specified range, while 8080.5 fails the integer rule. This is a policy chosen for the fictional lab, not a claim that all lower port numbers are invalid. When the condition is known to be false, Terraform reports the supplied message and stops the operation. If its inputs are unknown at plan time, checking may be deferred. A precondition does not roll back earlier infrastructure changes or establish that a real service is reachable.
Inspect outputs and protect stored data
Ordinary root outputs are recorded in state after apply. terraform output reads that stored result rather than reevaluating edited source files. A plan can preview known outputs, but does not commit them to state. In the approved local-only module-interface/ fixture, after reviewing and applying a plan, these commands would read the stored name:
terraform outputterraform output -raw application_nameterraform output -json application_nameThe raw result is app-catalog; the named JSON result is the JSON string "app-catalog". Raw mode is intended for scalar values, JSON for structured data. The command lists root outputs, not the child outputs independently.
The following synthetic example is a separate configuration illustrating redaction only:
output "demo_label" { value = "fictional-internal-label" sensitive = true}A sensitive marking hides the value in common plan/apply and all-output human-readable displays. It does not encrypt the state. Selecting a sensitive output by name or using raw/JSON modes can disclose it. Do not write real sensitive outputs into logs or share state or plan files. Ephemeral outputs, introduced in Terraform 1.10, are restricted to child modules and permitted ephemeral contexts; they are not a way to export an ordinary persistent root output without storing it. The exact inherited CLI pin remains untested.
Practice a public module contract
In the supplied module fixture, predict what happens if the root passes inventory instead of catalog. Then design a child module that accepts team, builds an internal label and exposes only a result called queue_name. Write the root expression that reads it. Identify one piece of information that should not be exposed as an output. Use the separate precondition fixture for boundary predictions. Next: expressions and operators.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Outputs and module interfaces — knowledge check
Check derived values and output interfaces.
How does a caller read output name from the naming module?
After editing a root output but before applying, what does terraform output read?
Which port fails the example precondition?
What does sensitive = true guarantee for an ordinary root output?
Which CLI format is suitable for a structured output object?