In this article, we will explore output variables in Terraform—a powerful feature for capturing and displaying the results of your configurations. Output variables in Terraform work in tandem with input variables, allowing you to easily reference and use the values of expressions from your infrastructure. For instance, if you want to capture the public IP address of an AWS EC2 instance, you can define an output variable to display this information.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Using output variables is especially useful when you need to pass dynamic information from Terraform into other automation tools such as ad-hoc scripts or Ansible playbooks.
AWS Instance Example with Output Variables
Below is a Terraform configuration that creates an AWS instance and then outputs its public IP address:- The resource block defines an AWS instance named “cerberus”.
- The
outputblock uses the keywordoutputfollowed by the variable namepub_ip. - The mandatory
valueargument references the public IP of the AWS instance. - An optional description provides clarity about the output.
Defining Necessary Variables
To complete the configuration, you need to define the necessary variables. Below is an example of how to define these variables:Viewing Output Variables
After running theterraform apply command, Terraform displays the configured output variable. This is an immediate way to verify the information, even if no changes have occurred in the configuration.
To view all defined outputs, run:
Output variables not only help display important resource details after deployment but also facilitate integration with other infrastructure-as-code tools.