Outputs

Output values expose useful data such as an IP address or database endpoint after apply.

Syntaxoutput "name" { value = resource.attribute }

Output values surface information about your infrastructure — a server's public IP, a load balancer's DNS name — after an apply. They are also how a module returns data to its caller.

Declaring an output

An output block has a value and an optional description. Mark it sensitive = true to hide it from the console.

Reading outputs

Run terraform output to see them, or terraform output -raw name to get a single value for scripts.

Example

Example · hcl
output "web_public_ip" {
  value       = aws_instance.web.public_ip
  description = "Public IP of the web server"
}

output "db_password" {
  value     = aws_db_instance.main.password
  sensitive = true
}

When to use it

  • A module outputs the created RDS endpoint so the calling root module can pass it as an environment variable to an ECS task definition.
  • A CI pipeline captures `terraform output -json` after apply to write a JSON artifact with the load balancer DNS name for downstream deployment steps.
  • An engineer uses a sensitive output to return a generated password from Terraform while ensuring it does not appear in plan or console logs.

More examples

Basic output value

Exposes the web instance's public IP as a named output, readable with terraform output after apply.

Example · hcl
output "instance_public_ip" {
  description = "Public IP address of the web instance"
  value       = aws_instance.web.public_ip
}

Sensitive output

Marks the output sensitive so Terraform hides it in normal logs; the value is still accessible via terraform output -raw.

Example · hcl
output "db_password" {
  description = "RDS master password"
  value       = random_password.db.result
  sensitive   = true
}

# Retrieve it explicitly:
# terraform output -raw db_password

Read outputs in CI pipeline

Shows how a CI script reads Terraform outputs as JSON and extracts specific values for post-deploy verification.

Example · bash
# After terraform apply
terraform output -json > tf_outputs.json

# Extract specific value
ALB_DNS=$(terraform output -raw alb_dns_name)
echo "Load balancer: $ALB_DNS"

# Pass to next step
curl -f "http://${ALB_DNS}/health"

Discussion

  • Be the first to comment on this lesson.