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.