Query data with output values
In the previous tutorial, you used input variables to parameterize your Terraform configuration. In this tutorial, you will use output values to organize data to be easily queried and displayed to the Terraform user.
When building complex infrastructure, Terraform stores hundreds or thousands of
attribute values for all your resources. As a user of Terraform, you may only
be interested in a few values of importance. Outputs designate which data to
display. This data is outputted when apply
is called, and can be queried
using the terraform output
command.
Define outputs
Define an output for the IP address of the instance that Terraform provisions. Create
a file called outputs.tf
with the following contents:
output "ip" { value = google_compute_instance.vm_instance.network_interface.0.network_ip}
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 the value, the network_ip
of the first network interface attribute of the compute instance.
Multiple output
blocks can be defined to specify multiple output variables.
Inspect outputs
You must apply this configuration before you can use these output values. Apply
your configuration now. Respond to the confirmation prompt with yes
.
$ terraform applygoogle_compute_network.vpc_network: Refreshing state... [id=projects/testing-project/global/networks/terraform-network]google_compute_instance.vm_instance: Refreshing state... [id=projects/testing-project/zones/us-central1-c/instances/terraform-instance] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: Terraform will perform the following actions: Plan: 0 to add, 0 to change, 0 to destroy. Changes to Outputs: + ip = "10.128.0.3" Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: ip = "10.128.0.3"
Now query the outputs with the terraform output
command.
$ terraform outputip = "10.128.0.3"
You can use Terraform outputs to connect your Terraform projects with other parts of your infrastructure, or with other Terraform projects. To learn more, follow our in-depth tutorial, Output Data from Terraform.
Destroy your infrastructure
Make sure to run terraform destroy
to clean up the resources
you created in these tutorials. When prompted remember to confirm with a yes
.
$ terraform destroygoogle_compute_network.vpc_network: Refreshing state... [id=projects/testing-project/global/networks/terraform-network]google_compute_instance.vm_instance: Refreshing state... [id=projects/testing-project/zones/us-central1-c/instances/terraform-instance] Terraform used the selected providers to generate the following executionplan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions:##...Plan: 0 to add, 0 to change, 2 to destroy. Changes to Outputs: - ip = "10.128.0.3" -> null Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes##...Destroy complete! Resources: 2 destroyed.
Next steps
That concludes the getting started tutorials for Terraform. Hopefully you're now able to not only see what Terraform is useful for, but you're also able to put this knowledge to use to improve building your own infrastructure.
For more hands-on experience with the Terraform configuration language, or to learn more of the building blocks of Terraform, review the tutorials below.
Configuration Language - Get more familiar with variables, outputs, dependencies, meta-arguments, and other language features to write more sophisticated Terraform configurations.
Modules - Organize and re-use Terraform configuration with modules.
Provision - Use Packer or Cloud-init to automatically provision SSH keys and a web server onto a Linux VM created by Terraform in AWS.
Import - Import existing infrastructure into Terraform.
To read more about available configuration options, explore the Terraform documentation.
Learn more about HCP Terraform
Although HCP Terraform can act as a standard remote backend to support Terraform runs on local machines, it works even better as a remote run environment. It supports two main workflows for performing Terraform runs:
- A VCS-driven workflow, in which it automatically queues plans whenever changes are committed to your configuration's VCS repo.
- An API-driven workflow, in which a CI pipeline or other automated tool can upload configurations directly.
For a hands-on introduction to the HCP Terraform VCS-driven workflow, follow the HCP Terraform getting started tutorials. HCP Terraform also offers commercial solutions which include team permission management, policy enforcement, agents, and more.