Jaswinder Singh
Jaswinder Singh
DevOps Engineer

Input Variable for Terraform Part-2

- Advertisement -

So, the following topics will be discussed in this:

Create your variable in a separate file

Keeping everything in the same file when dealing with a big number of variables might be difficult. As a result, it’s typical to isolate your variables into their own file.

Several approaches are available to allocate values to an input variable.

You can add a value can to an input variable in numerous ways that you can think of. When values didn’t have a default value, we used the interactive shell, so when we deployed an execution plan, we used the command line flag -var. You can also use environment variables and external files to set an input variable.

Mode of interaction

so,Terraform will ask you to provide a variable using interactive mode if you didn’t set a default value or enter the value using the -var flag.

Flags on the command line

Use the -var command-line flag followed by the variable’s name and value to provide a value.

$ terraform apply -var extension=txt -var pnfn=1124 -var psfn=file psfnSensitive=ITR

Variables in the environment

Environment variables can be put to use on a condition to begin with TF_VAR_ , followed by the name and value of a given variable.

$ export TF_VAR_content_example="unixcop"
$ export TF_VAR_extension="txt"
$ export TF_VAR_pnfn=1124
$ export TF_VAR_psfn="file"
$ export TF_VAR_psfnSensitive="ITR"
$ terraform apply

A file from another source

- Advertisement -

An external file would be used. If you have a lot of variables, using a command line or flags to set them all could lead to mistakes and take a long time. To help the procedure go faster and more efficiently, you can store them in a different file. Terraform core will read any file called terraform.tfvars and terraform.tfvars.json or ending in .auto.tfvars or .auto.tfvars.json as soon as you execute or apply a command.

If your file does not fit into one of these categories, you could still load it with the command flag:

$ terraform apply -var-file variables.tfvars

Important: Variables.tf should not be confused with variables.tfvars. The first provides the definitions of the input variables, while the second has their values. Terraform core would not load the values if you put them in a .tfvars file, and an error will be thrown.

Priority of variable definitions

Since you can now set the variable value in a variety of ways. You might be wondering which variable will be used by Terraform core at the time of carrying out the execution plan if you keep on assigning a value to the same variable using different techniques.

Terraform uses the precedence of individual variable definitions to determine which value to use and loads variables in the order listed below:

  • Variables in the environment.
  • Terraform.tfvars file.
  • Terraform.tfvars.json file.
  • In alphabetical order, any file ending in .auto.tfvars or .auto.tfvars.json.
  • Finally, the command-line flags -var and -var-file will be considered, which will take precedence and overwrite any earlier values.
Variables in Terraform output

Assume you’ve just set up the infrastructure to create a VM on Azure and wish to connect to it using a remote desktop connection. If you have the login and password, the very first step you need is the VM’s public IP. Because it’s generated dynamically during provisioning, you’ll have to retrieve it using the portal or Azure CLI. Terraform allows you to establish output variables to avoid doing all of these manual procedures and offer all of the info you need as output in the command line.

You should start creating a block of an output variable in the configuration or maybe an external file using this variable, and this block should have the respective attributes:

output "name_of_variable" {
   value       = Unixcop
   description = "short description"
   sensitive   = true|false

Value: The outcome of an expression that will be sent to the user.

Description: A brief explanation of variables and their function.

When set to true, sensitive protects data from being accidentally exposed in CLI and log output.


Keep Reading About Terraform


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook