Working with Outputs in Terraform
Discover how to create and utilize output variables in Terraform to fetch resource attributes such as an EC2 instance's public IP. Learn to manage outputs with options like sensitivity, dependencies, and preconditions to ensure secure and reliable infrastructure provisioning.
Overview
Previously, we looked at the flexibility that using variables, especially input variables, gives us. When we create resources in Terraform, these resources have unique attributes that can be passed on as output to create another resource. The syntax for creating an output variable is as below:
A practical use case
Let’s consider a situation where we have to create an EC2 instance and then obtain the public IP address so that we can initiate an SSH connection to the instance. We can create the needed resource and then use the output keyword to obtain the public IP address, as shown below:
Terraform outputs are available after a resource is created using terraform apply. The output can also be queried using the terraform output variable_name command. To query the output that we ...