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
- 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
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
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
- 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
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
Comments
Post a Comment