Get Started - Google Cloud

Output Variables

In the previous guide, you learned how to use input variables as a way to parameterize Terraform configurations. This guide introduces output variables as a way to organize data to be easily queried and shown back to the Terraform user.

When building complex infrastructure, Terraform stores hundreds or thousands of attribute values for all your resources. But as a user of Terraform, you may only be interested in a few values of importance, such as a load balancer IP, VPN address, etc.

Outputs are a way to tell Terraform what data is important. This data is outputted when apply is called, and can be queried using the terraform output command.

Defining Outputs

Let's define an output to show us the static IP address that we created. Create a file called outputs.tf with the following contents:

output "ip" {
  value = google_compute_address.vm_static_ip.address
}

This defines an output variable named "ip". The name of the variable must conform to Terraform variable naming conventions if it is to be used as an input to other modules. The value field specifies what the value will be, In this case, we're outputting the public_ip attribute of the elastic IP address.

Multiple output blocks can be defined to specify multiple output variables.

Viewing Outputs

Run terraform refresh to populate the output. This will refresh your state by comparing it to your cloud infrastructure. In the process, it will also pick up the new output. You should see output like this:

$ terraform refresh
google_compute_network.vpc_network: Refreshing state... [id=terraform-network]
google_compute_address.vm_static_ip: Refreshing state... [id=orbital-avatar-247819/us-central1/terraform-static-ip]
google_compute_instance.vm_instance: Refreshing state... [id=terraform-instance]

Outputs:

ip = 35.192.68.38

You'll also see outputs after running terraform apply, or you can query the outputs with terraform output.

To check a specific output, specify the output name in the command.

$ terraform output
ip = 104.154.236.90

The output command is also useful for scripts to extract outputs from your configuration.