Skip to main content
Welcome to this lesson on the Terraform output block. Outputs let you expose important data produced by your Terraform runs—such as IP addresses, DNS names, resource IDs, and connection strings—so people, scripts, and other modules can consume them without digging through cloud consoles or re-running queries.

Why use outputs?

  • Capture useful attributes created by Terraform (VM IPs, DB connection strings, DNS names, etc.).
  • Display important values immediately after terraform apply for quick testing.
  • Persist outputs in state so they can be retrieved later with terraform output.
  • Pass values between modules to enable composable infrastructure.
  • Make CI/CD automation and scripts more reliable using terraform output -json.

Example: Azure resources

These resource blocks create an Azure Resource Group and a Virtual Network. After creation, surface values such as the resource group name, VNet ID, or subnet IDs with outputs so other systems or modules can use them.
Use outputs to export values that are consumed outside this configuration—root modules, CI pipelines, or manual testers.

Basic output examples (AWS resources)

Below are typical outputs you might declare for an EC2 instance and a load balancer. These show simple attribute exposure, composed values, and how to produce a ready-to-use URL.
  • instance_public_ip: exposes a single resource attribute.
  • website_dns: exports the load balancer’s DNS name.
  • website_url: composes a click-ready HTTPS URL around a resource attribute.

Dissecting an output block

Each output block typically contains a few common elements: Use description to document intent and sensitive to prevent accidental exposure in terminal output.

Common terraform output commands

Use these commands locally or in automation:

Sensitive outputs

Mark outputs that contain secrets so Terraform hides them in interactive CLI output and most logs.
Mark outputs as sensitive = true for any secret or credential. Sensitive outputs are still recorded in the Terraform state (so secure your backend accordingly). Treat your state file as sensitive data.

Best practices and important considerations

  • Remember outputs are stored in state: protect your state backend and access control.
  • Limit outputs to values that are useful externally (URLs, IPs, IDs required by other systems).
  • Prefer secret managers for long-term secret storage; do not rely solely on outputs for sensitive secrets.
  • Use outputs to pass data between modules: a child module declares outputs that the parent (or other modules) can read.
  • Avoid exposing low-value or noisy attributes that are not consumed by other systems.
Do not store highly sensitive secrets only in outputs unless your state backend is secured and access is tightly controlled. Consider using dedicated secret management solutions for production secrets.

Quick reference table

Summary

  • output blocks provide a structured way to expose information from Terraform configurations.
  • Outputs appear after terraform apply, are stored in state, and can be consumed by scripts, CI pipelines, and other modules.
  • Use description and sensitive to make outputs easier and safer to use.
  • Keep outputs focused on externally useful values and secure your state backend.
Further reading and references:

Watch Video

Practice Lab