Capturing Resource Attributes
Consider a configuration that creates a random pet name using Terraform’s resource definitions. In this example, a resource called random_pet generates a pet name, and an output variable called pet-name captures the generated id. This is especially useful for passing data to other tools or for quick resource verification. Below is an example configuration:output followed by the variable name. Inside the block, the value argument is required and uses a reference expression (random_pet.my-pet.id). Although the desc argument is optional, it is a good practice to include a brief description of the output’s purpose.
Use meaningful descriptions for your output variables. This practice enhances code readability and aids in documentation, especially when collaborating with other team members.
Configuration of Related Variables
To support the resources defined above, ensure the following variables are declared in your configuration:Displaying Outputs with Terraform
When you runterraform apply, Terraform automatically displays the output variables after resource creation. For example:
terraform output command. Running the command without any arguments lists all outputs:
Output variables are invaluable for quickly viewing details about your provisioned resources and for integrating with other infrastructure as code tools, ad hoc scripts, or configuration management systems like Ansible.