0
0
TerraformHow-ToBeginner · 3 min read

How to Use Terraform Output: Syntax, Example, and Tips

Use output blocks in Terraform to declare values you want to see after applying your configuration. Run terraform output to display these values, which helps share important info like IP addresses or resource IDs.
📐

Syntax

The output block declares a value to show after Terraform applies your changes. It has a name to identify it and a value expression that defines what to display.

  • name: The label for the output.
  • value: The expression or resource attribute to output.
  • description (optional): Explains what the output means.
terraform
output "example_output" {
  value       = aws_instance.example.public_ip
  description = "The public IP address of the example instance"
}
💻

Example

This example creates an AWS EC2 instance and outputs its public IP address. After running terraform apply, you can run terraform output example_ip to see the IP.

terraform
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

output "example_ip" {
  value       = aws_instance.example.public_ip
  description = "Public IP of the example instance"
}
Output
example_ip = "54.210.123.45"
⚠️

Common Pitfalls

Common mistakes include:

  • Not running terraform apply before terraform output, so outputs are empty.
  • Referencing outputs that do not exist or have typos in their names.
  • Trying to output sensitive values without marking them sensitive = true, which can expose secrets.

Always check your output names and run apply first.

terraform
output "wrong_name" {
  value = aws_instance.example.public_ip
}

# Correct usage:
output "example_ip" {
  value = aws_instance.example.public_ip
}
📊

Quick Reference

CommandDescription
terraform outputShow all output values from the last apply
terraform output Show the value of a specific output
terraform output -jsonShow outputs in JSON format for automation
output blockDeclare outputs in your Terraform configuration
sensitive = trueHide output value from CLI to protect secrets

Key Takeaways

Declare outputs with output blocks to expose useful info after apply.
Run terraform apply before using terraform output to see values.
Use exact output names when querying specific outputs.
Mark sensitive outputs with sensitive = true to protect secrets.
Use terraform output -json for scripts and automation.