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.
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.- The output name (for example,
instance_public_ip) is the identifier you reference in the CLI, automation, or module consumers. valueis the expression Terraform evaluates and stores in the state; it can be a resource attribute or a composed expression.descriptionis 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:
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
descriptionfields for clarity. - Mark secrets as
sensitive = trueand 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 -jsonfor 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 outputor parse them withterraform output -jsonin automation.