Followers

Terraform - Output Variables

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

 

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

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
item
DevOpsWorld: Terraform - Output Variables
Terraform - Output Variables
https://jhooq.com/wp-content/uploads/terraform/terraform-output-locals/how-to-use-terraform-output-locals.webp
DevOpsWorld
https://www.devopsworld.co.in/2022/09/terraform-output-variables.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/09/terraform-output-variables.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content