Terraform - Output Variables

 

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_instancesaws_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 -

provider "aws" {
   region     = "eu-central-1"

}

resource "aws_instance" "ec2_example" {
   
   ami           = "ami-0767046d1677be5a0"
   instance_type = "t2.micro"
   subnet_id = aws_subnet.staging-subnet.id
   
   tags = {
           Name = "test - Terraform EC2"
   }
}

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

output "my_console_output" {
  value = aws_instance.ec2_example.public_ip
} 
BASH
  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_arnpassword_dataprimary_network_interface_idprivate_dnspublic_dns etc.



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 "my_console_output" {
  value = aws_instance.ec2_example.public_ip
} 
BASH

Your directory structure should look like this -

How to use terraform output locals?



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 -

output "my_console_output" {
  value = aws_instance.ec2_example.public_ip
  sensitive = true
} 
BASH

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

Comments