Q: What does `terraform output` do?
terraform output reads the output values of the root module from the current state and prints them, without planning or changing any infrastructure.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: <code>terraform output</code> reads the output values of the root module from the current state and prints them, without planning or changing any infrastructure. It is the supported way for scripts, CI jobs and humans to consume values such as a load balancer DNS name or a cluster endpoint. Interviewers use it to check that you understand where outputs live (state, root module only), how sensitive values behave, and the difference between <code>-json</code> and <code>-raw</code> when you wire Terraform into shell pipelines.
What terraform output reads and what it does not
Outputs are computed during apply and stored in state; the command just reads them. Knowing this boundary explains most surprises, such as an output that exists in code but returns nothing yet.
output "alb_dns_name" {
description = "Public DNS name of the load balancer"
value = aws_lb.web.dns_name
}
output "db_password" {
value = random_password.db.result
sensitive = true
}
- Source: the root module's
outputblocks as recorded in the state of the current backend. It does not call cloud APIs and does not refresh. - Root only: outputs of child modules are not shown unless the root module re-exports them with its own
outputblock. - No state, no output: before the first successful apply you get a warning that no outputs were found.
- Cross-stack use: another configuration can consume the same values through the
terraform_remote_statedata source, which also exposes root outputs only. - Check before apply: because outputs come from state, a new output block does not appear in
terraform outputuntil you runapply(orapply -refresh-only) to store it.
Hands-on: terraform output in scripts
Pick the format that matches the consumer.
terraform output
terraform output alb_dns_name # "my-alb-123.us-east-1.elb.amazonaws.com"
URL=$(terraform output -raw alb_dns_name)
curl -fsS "https://$URL/healthz"
terraform output -json | jq -r '.alb_dns_name.value'
terraform output -json subnet_ids | jq -r '.[]'
- All outputs:
terraform outputprints every output in a human readable form; sensitive ones show as<sensitive>. - One value:
terraform output alb_dns_nameprints a single output; strings are quoted in this mode. - -raw: prints a string, number or bool with no quotes or newline decoration, ideal for shell variables. It fails for lists and objects.
- -json: prints machine readable JSON for every output (or one) and is the right choice with
jq.
Sensitive outputs and what they really protect
The sensitive flag controls display, not storage. Treat the sensitive flag as a courtesy to anyone reading terminal output, not as encryption.
- Display: with
sensitive = truethe plan and the plainterraform outputlist hide the value. - Explicit read: naming a sensitive output, or using
-jsonor-raw, still prints it. Do not run those in logged CI steps without masking. - State exposure: the value is stored in plaintext in the state file, so restrict and encrypt the backend and prefer a secrets manager for real secrets.
- Propagation: a value derived from a sensitive input must itself be marked sensitive or Terraform raises an error.
- Cleaning up: pipe sensitive values straight into the consuming command or a secure store, never into a log line, an artifact or a pull request comment.
Failure modes and edge cases
Most issues come from the wrong directory, backend or output type. Always confirm which backend and workspace you are reading before trusting what comes back.
- Wrong working directory or workspace: you read another environment's outputs. Confirm with
terraform workspace showand the backend config. - Backend not initialised: run
terraform initfirst so the remote backend is configured. - Stale values: outputs reflect the last apply. If infrastructure changed outside Terraform, run
terraform apply -refresh-onlyto update state first. - -raw on a list: fails with an error about unsupported type; use
-jsonandjq. - Exit codes in scripts:
terraform outputreturns a non-zero code when the named output does not exist, so useset -ein shell steps and fail loudly.
Using outputs safely in pipelines
Treat outputs as an interface between stacks and jobs. Outputs form a small public interface for your stack, so manage them like an API.
- Stable contract: document output names and types; renaming one breaks downstream scripts and remote state consumers.
- CI masking: mask values written to job variables and avoid echoing them.
- Prefer data sources: for cross-stack lookups, a data source against the real resource is often less coupled than reading another stack's state.
- Precondition style checks: in CI, run
terraform output -jsonafter apply and assert the expected keys exist before promoting the build to the next stage. - Documentation: add a
descriptionto each output so the purpose and expected consumers are visible to the next engineer.
- terraform output prints the root module's output values from the current state, with no refresh or infrastructure change.
- I verify I am in the right workspace and backend, and use -raw for strings and -json for structured values.
- I automate downstream steps from outputs in CI, mask sensitive ones, and keep a stable output contract.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Expecting child module outputs to appear Only the root module's outputs are shown; re-export them explicitly.
- Using -raw with lists or objects The command errors because -raw supports only strings, numbers and booleans.
- Believing sensitive = true encrypts the value It only hides it in CLI display; the state file still contains it.
- Reading outputs from the wrong workspace Scripts then act on another environment's endpoints.