Outputs
An output is a named value that Terraform prints after an apply. It is the way a configuration hands a value back to the reader.
Declare an output
The value points at a resource attribute.
output "file_path" {
value = local_file.report.filename
}Show outputs again
Run this any time after an apply.
terraform outputHide a sensitive value
Sensitive outputs print as hidden in the terminal.
output "secret_note" {
value = var.note
sensitive = true
}Example
# main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.0"
}
}
}
resource "local_file" "report" {
content = "Report ready."
filename = "${path.module}/report.txt"
}
output "file_path" {
value = local_file.report.filename
}
output "greeting" {
value = local_file.report.content
}
# commands
terraform init
terraform apply -auto-approveKey points
- An output block names a value. The value can come from any resource.
- Terraform prints outputs at the end of an apply. They are easy to spot.
- The terraform output command shows them again later. There is no need to apply twice.
- Outputs can feed another configuration. Remote state reads them from the backend.
- A sensitive output hides its value in logs. The value still exists in state.
The apply creates the file and then prints both outputs. The path and the content come straight from the resource. Run terraform output later to see them again without applying.
Note
an output never changes infrastructure. It only reports a value.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does the terraform output command do?