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.
Note
This tutorial builds on configuration from an earlier tutorial. If you have not yet completed it, do so now.
Define an output
Create a file called outputs.tf
in your learn-terraform-azure
directory. Add the following output definition to outputs.tf
.
outputs.tf
output "resource_group_id" { value = azurerm_resource_group.rg.id}
This defines an output variable named resource_group_id
. 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 id
attribute of your resource group.
You can define multiple output
blocks to specify multiple
output variables.
Observe your resource outputs
When you add an output to a previously applied configuration, you must re-run terraform apply
to observe the new output.
Apply your configuration. Remember to confirm the apply with a yes
.
$ terraform apply ## ... Changes to Outputs: + resource_group_id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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: resource_group_id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup"
Notice that the apply
run returns the outputs. Query the output using the output
command with the output ID.
$ terraform output resource_group_id"/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup"