How to use Terraform output values?



In this article we are going to look on terraform output values. Terraform output values will be really useful when you want to debug your terraform code. Terraform output values can help you to print the attributes reference(arn, instance_state, outpost_arn, public_ip, public_dns etc) on your console.

The typical syntax of Terraform output values is -

1output "random_output" {
2value = "Hello this is output"
3}

Once you run your terraform apply command, it is going to print Hello this is output onto your console.

But there are more than just simply outputting static text values with terraform output variables. You can do a lot more.

Table of Content

  1. How to print the public_ip of aws_instance?
  2. How to create output.tf for terraform output values?
  3. How to prevent printing sensitive info on the console?


1. How to print the public_ip of aws_instance?

This is one of the most classic examples for terraform output values. As we know terraform is used as Infrastructure as code, so with the help of terraform you can provision many resources such - aws_instances, aws_vpc etc.

But is there a way by which you can know the public_ip address of the instance which you are planning to provision using terraform?

Yes, there is a really convenient and easy way to achieve that.

Here is my aws_instance which I have defined as inside my main.tf -

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2G2LXXXXX"
 4   secret_key = "r1oaShokKPw+YY7qaHxj8mD2T8BpxRUVXXXXXXXX"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8   
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type = "t2.micro"
11   subnet_id = aws_subnet.staging-subnet.id
12   
13   tags = {
14           Name = "test - Terraform EC2"
15   }
16}

You can simply append following terraform output value code inside your main.tf file so that it can print the public ip of your aws_instance

1output "my_console_output" {
2  value = aws_instance.ec2_example.public_ip
3} 

Break down of the above script

  1. aws_instance - It is the keyword which you need write as is.
  2. ec2_example - The name which we have given at the time of creating aws_instance
  3. public_ip - You can use attributes reference page to get all the attribute which you want to print on console.

The same approach can be followed to print - arn,instance_state,outpost_arn, password_data, primary_network_interface_id, private_dns, public_dns etc.


2. How to create output.tf for terraform output values?

In the previous point we have seen how to create your first terraform output values. But if you have noticed we have created the terraform output values inside the same main.tffile.

It is completely okay to create terraform output values inside the main.tf but this practice is certainly not recommended in the industry.

The recommended way is to create a separate output.tf specially of the terraform output values, so that all the output values can be stored there.

The only change which you need to do over here is to create a new output.tf and store the following terraform code init -

output.tf

1output "my_console_output" {
2  value = aws_instance.ec2_example.public_ip
3} 

Your directory structure should look like this -


3. How to prevent printing sensitive info on the console?

As terraform output values help us to print the attributes reference values but sometimes you can not print all the values on console.

So to prevent from printing sensitive values on the console you need to set sensitive = true.

Here is an example of the terraform code where we want to prevent showing the public ip to console -

1output "my_console_output" {
2  value = aws_instance.ec2_example.public_ip
3  sensitive = true
4} 

In the above code if you noticed we are using sensitive = true which tells terraform not to show public ip on console.

Conclusion

Hope the above article will help you to build your understanding on terraform output values.

With the combination of terraform variable, terraform locals you really benefit well on terraform output values

You can read more on terraform output values over here


Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?