Terraform Tutorial — Output Block Syntax

1 minute read

The output block declares a value that Terraform displays after apply and stores for later querying.


Basic Output Block

output "bucket_arn" {
  value       = aws_s3_bucket.main.arn
  description = "ARN of the S3 bucket"
}

After terraform apply:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

bucket_arn = "arn:aws:s3:::my-terraform-bucket-12345"

Output Block Attributes

Attribute Required? Purpose
value ✅ Yes The expression to evaluate and display
description Recommended Human-readable explanation
sensitive Optional Hides the value from plan/apply output
depends_on Optional Explicit dependency (rarely needed)
precondition Optional Validation that runs before output is evaluated

Multiple Outputs

Define all outputs in outputs.tf:

# outputs.tf

output "bucket_name" {
  value       = aws_s3_bucket.main.id
  description = "Name of the S3 bucket"
}

output "bucket_arn" {
  value       = aws_s3_bucket.main.arn
  description = "ARN of the S3 bucket"
}

output "bucket_region" {
  value       = aws_s3_bucket.main.region
  description = "Region where the bucket was created"
}

Output with Expressions

Outputs can contain any valid Terraform expression:

output "public_urls" {
  value = [
    for instance in aws_instance.web :
    "http://${instance.public_ip}:8080"
  ]
  description = "URLs of all web server instances"
}

output "resource_summary" {
  value = {
    bucket_name = aws_s3_bucket.main.id
    vpc_id      = aws_vpc.main.id
    subnet_ids  = aws_subnet.public[*].id
  }
  description = "Summary of all created resources"
}

Output Preconditions

Validate assumptions about the output:

output "instance_ip" {
  value       = aws_instance.web.public_ip
  description = "Public IP of the web server"

  precondition {
    condition     = aws_instance.web.public_ip != ""
    error_message = "The instance was not assigned a public IP. Check subnet settings."
  }
}

What’s Next?

You’ve declared outputs. Let’s learn how to query and use them from the command line.


← Outputs Hub Querying Outputs →