HashiCorp Learn
Infrastructure
  • TerraformTerraformLearn terraformDocs
  • PackerPackerLearn packerDocs
  • VagrantVagrantLearn vagrantDocs
Security
  • VaultVaultLearn vaultDocs
  • BoundaryBoundaryLearn boundaryDocs
Networking
  • ConsulConsulLearn consulDocs
Applications
  • NomadNomadLearn nomadDocs
  • WaypointWaypointLearn waypointDocs
  • HashiCorp Cloud Platform (HCP) LogoHashiCorp Cloud Platform (HCP)HashiCorp Cloud Platform (HCP)Docs
Type '/' to Search
Loading account...
  • Bookmarks
  • Manage Account
  • Overview
  • Define an output
  • Observe your resource outputs
DocsForum
Back to terraform
AzureView Collection
    Introduction to Infrastructure as Code with TerraformInstall TerraformBuild InfrastructureChange InfrastructureDestroy InfrastructureCreate Resource DependenciesDefine Input VariablesQuery Data with Output VariablesStore Remote State

Query Data with Output Variables

  • 1 min
  • Products Usedterraform

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


PreviousDefine Input VariablesNextStore Remote State
HashiCorp
  • System Status
  • Terms of Use
  • Security
  • Privacy
stdin: is not a tty