Terraform Tutorial · Terraform Basics · Lesson 7 of 25
Terraform Outputs
Outputs expose values from a configuration: to the command line, to scripts, and to other configurations and modules.
Declaring outputs
locals {
servers = ["web-1", "web-2", "web-3"]
}
output "server_count" {
description = "How many servers we run"
value = length(local.servers)
}
output "first_server" {
value = local.servers[0]
}
output "api_token" {
value = "s3cr3t-token"
sensitive = true
}
Output
api_token = <sensitive> first_server = "web-1" server_count = 3
Run this on your computer — see “Get Started” for setup.
After terraform apply, read them with terraform output or, for scripts, terraform output -json or
terraform output -raw db_endpoint.
Typical outputs
output "load_balancer_dns" {
description = "Public address of the app"
value = aws_lb.app.dns_name
}
output "db_password" {
value = random_password.db.result
sensitive = true # shown as (sensitive value)
}
In a module, outputs are the module's return values: the caller reads them as
module.network.vpc_id.
Test yourself
How does a script read a single output value without quotes?
terraform show NAMEterraform output -raw NAMEterraform state NAMEterraform get NAME
Show answer
Answer: terraform output -raw NAME