Let's think about this for a second
After a resource is created, there are values auto-generated by Terraform/the cloud provider (for example, a server's public IP address, or a database's connection endpoint) — if you want to see these values in the terminal right after apply finishes, use an output block (`output "name" { value = ... }`). Outputs are also widely used to pass data between modules (later lesson) — a parent module can consume a child module's output as its own input.
Let's connect this to a real scenario
If you define `output "server_ip" { value = aws_instance.web.public_ip }`, you'll immediately see `server_ip = "54.123.45.67"` in the terminal after `terraform apply` finishes — no more manually hunting down the IP address in the cloud console just to SSH in. Running the `terraform output` command any time after apply lets you see all output values again.
Let's look at an example
resource "aws_instance" "web" {
ami = "ami-0abcdef1234567890"
instance_type = "t3.micro"
}
output "server_ip" {
description = "Public IP address of the web server"
value = aws_instance.web.public_ip
}
output "server_id" {
value = aws_instance.web.id
}$ terraform apply
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
server_id = "i-0123456789abcdef0"
server_ip = "54.123.45.67"5-minute try-it
Add 2 output blocks (server_ip, server_id) and apply — then run `terraform output` separately and check the values.
A quick word of caution
Add the `sensitive = true` argument to any sensitive output — Terraform will hide the value in terminal output as `(sensitive value)` (though it still appears in the state file).