When creating complex infrastructure, Terraform stores all attribute values for all your resources. But you may only be interested in a few values, such as a load balancer IP, VPN address, etc.
Outputs define values that will be highlighted to the user when Terraform applies and can be queried easily using the “output” command.
Defining Outputs
Syntax
output NAME {
value = VALUE
}
Let’s define an output to show public DNS address
output "address" {
value = ["${aws_instance.web.public_dns}"]
}
Sensitive Outputs
Outputs can be marked as containing sensitive material by setting the sensitive attribute to true, like below:
output "sensitive" {
sensitive = true
value = VALUE
}
Viewing Outputs
When terraform command executed, it will populate the output as below.
$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
ip = 150.23.245.171
You can also query the outputs after apply-time using terraform output:
$ terraform output ip
150.23.245.171