القائمة الرئيسية

الصفحات

Output variables in Terraform

 What are Output variables?


- We will discuss an other kind of variables in Terraform, known as output variables


- Let's understand first of all, why and when we need output variables?


What we need output variables?


- we have seen that to generate a random_integer with terraform, we need a resource block of type random_integer which belongs to the random provider


- the required arguments for the random integer resource are only the min and max arguments


- after iniatializing the provider and running the terraform apply command, we see the min and max arguments that we have set in the configuration file are displayed in the output of the terraform apply command, but we didn't get the random value that will be generated by terraform


- The value will be stored in the result attribute of the random integer resource but it can be known only after applying the configuration


Generate a random integer with terraform



- the same when we provision an aws instance with terraform


- to create an aws instance with terraform we make use of the aws provider, and the resource type  that we need here is aws_instance


- the required arguments here are the AMI which is the amazon machine image, and the instance type


- many attribute of the AWS instance resource will be known only after creating the AWS instance, and they will be not shown in the output of the Terraform apply command


Provision an aws instance with terraform



What are output variables?


- In this situation, we may need output variables


- output variables can be useful if you want to make information available on the command line when running the terraform apply command or also to make certain information accessible for other configuration scripts or tools


- Let's see now how to declare an output variable


- To define an output variable you need first off all to add a block of type "output", the output keyword here will let terraform know that this block is used for declaring an output variable


- the label immediately after the output keyword is the name giving to the output variable. this name can be used later to access the value that will be stored in this variable


- inside the output block, we set the value argument to an expression whose result will be stored in this output variable


- you can declare output variables in same file with the resources that you want to provision or you can declare them in a separate file called for example outputs.tf


Declaring output variables in Terraform



Examples of using output variables


- Let's go back the our first example and see how we can display the random integer on the command line using output variables, when running the terraform apply command


- we have here the same configuration file that we have used to generate a random integer


- and we have added a block of type output, so this block is used to define an output variable, and the output variable is called generated_value


- the value argument of this output variable is set to the result attribute, of the rand_int resource which is a resource of type random_integer created using the random provider


- So here terraform will first of all create a random_integer resource called rand_int, and generate a random integer that will be stored and accessed using the result attribute of the rand_int resource


-  and because here we have set the output variable called generated_value to the result attribute of the rand_int resource,the random integer that will be generated will be stored in this output variable


- now when we run the Terraform apply command, all the output variables with the values associated with each one will be display on the command line 


Displaying the random integer using output variables



- The same thing if we want to display the public IP of the AWS instance on the command line, when running the Terraform apply command


- in addition to the resource block used to provision an AWS instance, we will add another block of type output to declare an output variable called instance_public_ip


- in this block, we set the value argument to aws_instance which is the resource type, .instance which the name given to the resource that we want to create, .public_ip, the attribute of the instance resource where the public ip of the aws instance will be stored


- By running the terraform apply command now, terraform will create the instance and will display the public_ip of the instance in the outputs section, in the output of the terraform apply command


Displaying the public ip using output variables



Terraform output command


- another way to display output variables on the terminal is using the terraform output command


- by running this command, terraform will display all the output variables with the values associated with each one


- If you want to get just one output variable, all you have to do is to specify the name of the output variable to the terraform output command, as we did here with the instance_public_ip variable


Displaying output variables using the terraform output command



Custom condition check


- When declaring an output variable, we can use the preconditon block argument to add some custom conditions checks

- In the example that we have here, we set the conditon argument of the precondition block, to random_integer.rand_int.result greater than 15

- So If the generated value is greater than 15, it will be displayed on the terminal, otherwise you will get an error indicating that the condition is not met, and the generated value is less than 15


Custom condition check



Output variable block arguments

- in addition to the value and precondition arguments, we can use the description argument when declaring an output variable

- Like input variables,  the description argument can be useful if you want to describe the purpose of using this variable and the value that will be stored in it

Output variables - the description argument



- If the output variable is used to stored sensitive information, you can set the sensitive argument to true and this will hide the value stored in the variable when running the terraform apply command or the terraform output command without specifying the name of the output variable

- but if you run the terraform output command, followed by the name of the output variable, the value stored in this variable will be displayed on the terminal even if we set sensitive to true

Output variables - The sensitive argument



- You can learn more about output variables from this video




- so that's is for this lecture and see you in the next one 

Comments