Dot11 Guru

automation, scripting, Tools & Scripts

Terraform Associate Certification – Part 3

Input Variables

As a best practice, define your variable blocks in an external file. The name of the file is not important, but it should be something easy to recognise. When using variables in your configuration blocks, you don’t need to wrap them with quotation marks.

A variable can be named anything except: source, version, providers, count, for_each, lifecycle, depends_on and locals. If the variable contains sensitive information, you can use the sensitive argument. This argument is a boolean, if set to true the value is not displayed in plan or apply, but is shown in the state

examples of variables

If you do not specify the default value in the variable block, like filename above, you will be prompted to input when you execute the configuration. The description argument is optional but should always be used.

Variable Types

Use the type argument to define the type of data that is allowed.

String: A string can contain any alphanumeric character,

Number: A number is a integer,

Boolean: A boolean can contain true or false,

Any: If no type is set, the default setting is “Any”, which can contain any character,

List: A list contains multiple items, [item1, item2], when calling a list, the first entry is 0,

Map: A map is built of key value relationships {item1 : value1 ; item2 : value2}. To match a map, call the value by var.variableName[“Key”],

Object: An object can contain all of the above types of variables

Tuple: A tuple is like a list, but a tuple can contain different types within it.

Set: A set is like a list, but cannot contain duplicate values

Example of an object

Variable Validation

A validation block can exist inside of a variable block which defines the condition and a error message.

As Type Conversion

There are a number of options available to convert an argument to a type of value.

tobool: Converts the argument to a boolean. Only true, false, null are allowed.

tolist: Converts the argument to a list.

tomap: Converts the argument to a map.

tonumber: Converts the argument to a integer, only numbers are allowed.

toset: Converts the argument to a set.

tostring: Converts the argument to a string.

Variable Precedence

Variables are loaded using the following precedence, top to bottom.

Enviroment Variables: You can pass variables via environmental variables, by exporting with the prefix TF_VAR_

terraform.tfvars: Variables inside of this file are considered second.

*.auto.tfvars: Variables contained in files with this suffix are considered third. If there are multiple files with this suffix, it is processed alphabetically.

-var or -var-file: Using this argument when executing the apply or plan will result in the variables being processed last.

Output Variables

Output variables are defined in a block, and cannot be reused inside the config file. These variables are not shown during the plan command. They can be seen by issuing the terraform output command, or through the apply command.

Output variables are typically used for passing variables to other platforms like Anisble or bash scripts.

When defining an output variable block, you have to atleast define the value.

The following optional arguments are available:

description: Provide a description of the output variable

sensitive: When set to true, the variable will not be shown during the apply, show, output commands. The variable is viewable in the state file.

depends_on: Defines the explicit dependency.

Share this:

Leave a Reply