What are Outputs?

Outputs = Get information AFTER resources are created

Why use them?


Basic Syntax

output "output_name" {
  value       = resource.name.attribute
  description = "What this output shows"
  sensitive   = false  # Set true to hide sensitive data
}

Access: var.output_name or shown after terraform apply


Example 1: Get Public IP

main.tf

resource "aws_instance" "myserver" {
  ami           = "ami-12345"
  instance_type = "t3.micro"
}

output "server_public_ip" {
  value       = aws_instance.myserver.public_ip
  description = "Public IP address of the server"
}

After terraform apply:

Outputs:

server_public_ip = "54.123.45.67"

Example 2: Multiple Outputs

main.tf

resource "aws_instance" "web" {
  ami           = "ami-12345"
  instance_type = "t3.micro"

  tags = {
    Name = "WebServer"
  }
}

output "instance_id" {
  value       = aws_instance.web.id
  description = "Instance ID"
}

output "public_ip" {
  value       = aws_instance.web.public_ip
  description = "Public IP address"
}

output "private_ip" {
  value       = aws_instance.web.private_ip
  description = "Private IP address"
}