When building complex infrastructure, Terraform stores hundreds or thousands of attribute values for all your resources. Parsing these resources and attributes quickly becomes unsustainable for you as an operator.
Outputs are a way to tell Terraform what data is important.
Terraform outputs these values after an apply operation and you
query these values using the terraform output
command.
»Define an output
Add this output block to your main.tf
file in your learn-terraform-azure
directory. If you are starting this tutorial without completing the previous ones, clone the example GitHub repository for the complete configuration.
output "public_ip_address" {
value = data.azurerm_public_ip.ip.ip_address
}
This defines an output variable named public_ip_address
. 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, and almost always contains
one or more interpolations, since the output data is typically
dynamic. Upon a successful apply operation, Terraform outputs the public IP address of the elastic load balancer in the configuration.
Multiple output
blocks can be defined to specify multiple
output variables.
»Observe your resource outputs
When you add an output to a previously applied configuration, you must re-run the apply job to observe the new output.
Apply your configuration with the required username and password input variables specified. Remember to confirm the apply with a yes
.
$ terraform apply -var 'admin_username=plankton' -var 'admin_password=Password1234!'
## ... Output Truncated ...
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Outputs:
public_ip_address = 52.183.66.147
Notice that the apply
run returns the outputs. Query the output using the output
command with the output id.
$ terraform output public_ip_address
52.183.66.147