Explaining Terraform Output Variables

The aim of this page📝 is to explain outputs in terraform. Outputs are one of the three variable types in Terraform, the other 2 being input variables and locals.

Pavol Kutaj
1 min readSep 19, 2023

outputs have 3 basic properties: printed at the end; stored in terraform state files; exposed in module

  • outputs are printed to the terminal window at the end of the configuration run
  • terraform stores output values in the state data
  • output data is used to expose values when a configuration is placed in a module

output syntax is a combination of output keyword with 2 requirements: <name_label> and <value>

""" SYNTAX """
output <name_label> {
# required
value = <value>
# optional
description = <string>
sensitive = <bool>

}
  • the value of output can be any terraform-supported data type
  • description is the best practice
  • sensitive - the value will not be printed to the terminal, used for secrets

same as for variables and locals, outputs deserve its own file, usually called outputs.tf

  • add outputs.tf into the tf config directory
# outputs.tf
output "public_dns_hostname" {
value = "http://${aws_instance.nginx1.public_dns}"
description = "Public DNS hostname of web server"
}
  • After running terraform apply, you’ll get the values of output at the very end of the output
#...logs
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.

Outputs:

public_dns_hostname = "http://ec2-3-79-190-56.eu-central-1.compute.amazonaws.com"

LINKS

--

--

No responses yet