Skip to main content
Welcome to this lesson on the Terraform output block. This guide explains why outputs matter, how to define them, and practical patterns to expose configuration values for humans and automation. Outputs are the structured way to retrieve important information created by Terraform—IP addresses, DNS names, connection strings, resource IDs, and other attributes you need after provisioning. They also serve as the primary mechanism for passing data between modules and integrating Terraform with automation workflows.
Outputs are useful both for immediate feedback after terraform apply and for programmatic access in CI/CD pipelines. Use outputs to avoid manual lookups in provider consoles.
When Terraform provisions resources — virtual machines, databases, storage accounts, load balancers, DNS records, and so on — you frequently need to access attributes of those resources afterward. Instead of manually hunting through a cloud provider console, declare an output to return that value and have Terraform display or store it. Example resource definitions (Azure) that might create values you want to expose:
A purple presentation slide titled "Output Block" describing Terraform output usage, with explanatory text on the right. On the left is a framed, stylized photo of a partially open laptop lit in purple and blue.

Common output block patterns

Below are common output examples in HCL. They show how to expose a resource attribute directly and how to construct a friendly URL from resource attributes.
Key points about outputs:
  • The output name (for example, instance_public_ip) is the identifier you reference in the CLI, automation, or module consumers.
  • value is the expression Terraform evaluates and stores in the state; it can be a resource attribute or a composed expression.
  • description is optional but recommended — it documents the purpose for other users and tools.

Use cases and examples

Outputs are stored in the Terraform state, enabling retrieval without re-running terraform apply. They are also the standard way to pass values between modules.

Inspecting and querying outputs

  • List all outputs from current state:
  • Query a single output value:
For automation, many CI/CD systems and wrapper tools read outputs directly from the state file or use the terraform output -json flag to parse outputs programmatically:

Handling sensitive outputs

If an output contains sensitive information (passwords, secrets, private endpoints), mark it as sensitive to prevent it from being printed in CLI output or exposed in logs:
Mark any outputs that contain secrets as sensitive = true. Sensitive outputs are suppressed from normal CLI output and help reduce accidental exposure.

Best practices

  • Export only values you actually need. Avoid exposing unnecessary internals from modules.
  • Use descriptive names and description fields for clarity.
  • Mark secrets as sensitive = true and manage sensitive values via secure variable sources (e.g., secret managers).
  • Prefer passing complex or multiple values using maps or objects when modules expect structured input/output.
  • Use terraform output -json for robust automation and parsing.

Quick checklist

  • Use outputs to expose values needed after provisioning.
  • Use outputs to pass data between modules.
  • Mark secrets as sensitive to protect confidential values.
  • Query outputs with terraform output or parse them with terraform output -json in automation.
Further reading and references: This concludes the lesson on Terraform output blocks. Outputs make it easier to verify deployments, wire modules together, and integrate Terraform into automated workflows.

Watch Video

Practice Lab