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

الصفحات

Setting input variables in Terraform

 How can we set input variables in Terraform?



- We have seen earlier that input variables can be declared like this without setting any argument and without providing a default value for the variable


- Let's see now what happens if we run terraform apply command in such a case and the different ways for supplying values for input variables


Input variables without default values



- We have here two terraform configuration files, on called variables.tf for declaring input variables and an other called main.tf for defining the resources that we want to create


- in the variables.tf file, we have two input variables one called filename and another called content, but we didn't set any argument for both variables


- in the main.tf file, we have used these variables for setting the arguments of the local_file resource


- Since we didn't give any default values for these variables, when we run the terraform apply command, terraform will ask us to enter a value for each input variable and using these entered values, terraform will create, update and destroy resources as you mention in the configuration file


Entering values on the command line



- when input variables are declared in your configuration, they can be set in different ways


Setting input variables on command line


- first of all, on the command line


- You can set a variable on the command line in an interactive mode when running the terraform apply command using the -var option followed by the variable name and the value that you want to assign to this variable


- if you have several variables that need to be set, you can use the -var option multiple times in a single command


- We set here two variables on the command line: we assign the value weclome to the content variable and the value file.txt to the content variable using the -var option for each variable


- And using these values, Terraform will make the infrastucture up to date as mentioned in the configuration file 


Setting input variables on the command line



Variable definitions files


- Setting variables on the command line may be not convinient, espacially if you have a lot of variables that need to be set


- a more convinient way in this sitution is to set them using variable definitions files


- a variable definitions file is a file that ends with .tfvars or .tfvars.json where you can set multiple variables. it's very similar to any terraform configuration file, but it consists only of variable name assignments


- if the file is named exactly terraform.tfvars or terraform.tfvars.json or with any name that ends with .auto.tfvars or .auto.tfvars.json, terraform will load the file automatically when applying your configuration by running the terraform apply command and there is no extra work that you have to do


Variable definition file loaded automatically by terraform



- otherwise you have to specify the variable definitions file when running the terraform apply command using the -var-file option followed by the name of the variable definitions file


- Here the file is called values.tfvars, so the file will not be loaded automatically by terraform and you should explicitly specify the file using the -var-file option to enable terraform to use these values when creating the resources


Variable definition file specified using the -var option



Using environment variables


- another way for setting input variables in Terraform is throw environment variables.


- When we run the terraform apply command, Terraform will search for environment variables called TF_VAR_ followed by the name of a declared variable


- an input variable can be set using environment variables like this: we export an environment variable called TF_VAR_ ending with the name of the declared variable, followed by the value that we want to assign for this variable.


- For example for setting the filename variable using environmnet variables, we will export an environment variable called TF_VAR_filename, followed an equal sign, followed by the value that we want to assign to this variable.


- And the same for the content variable.


- now, we run the terraform apply command, terraform will search for environment variables starting with TF_VAR_, it will find two environment variables, one for setting the filename, and an other for setting the content.


- so it will use the giving values, for creating a resource of type local_file.


- so here a file called welcome.txt will be createdn with the content "welcome to Terraform".


Setting input variables using environment variables



Variable definition precedence


- So these are all the methods that we can use for setting input variables


- We can set a variable in different places, for example in a variable definitions file and using environment variables and also using the -var option,  but this can lead to unexpected results, if we didn't know how terraform will take these values.


- here for example, we assign the value file.txt to the filename variable using an environment variable.


- in terraform.tfvars, it is set to my_file.txt 


- and in another variable defintions file called values.auto.tfvars, we set it to welcome_file.txt


- and finally in the data.tfvars, it is set to welcome.txt, that we have passed to the terraform apply command using the -var-file option


- The question that you may ask here: which value will be taken when several values are provided? 


assigning different values to the same input variable



- to answer this question, we have to understand first of all variable definition precedence in Terraform?


- When we run the terraform apply command, Terraform looks first of all if we have set input variables using environment variables.


- then it loads the values assigned using the terraform.tfvars and if it exists, it will override the values assigned using environment variables


- then if a file called terraform.tfvars.json exists, the values assigned here will override those that are assigned in the terraform.tfvars or using environment variables


- then terraform will look if variables definitions files ending with .auto.tfvars or .auto.tfvars.json exist. if yes, they will proccessed in their lexical order.

- and finally, the values assigned using the -var option or the -var-file option which will override any previous values 


Variable definition precedence




- So if we go back to our previous example, here we have used the -var-file option which has the highest precedence, therefore terraform will ignore all the values and will take the values mentioned in the data.tfvars file

How terraform behaves when multiple values are given?



You find more information about variable definition precedence in this video



Comments