A variable is something which can hold a value of a particular type that we usually use to make our configuration dynamic by passing those variables as parameters.
Variable Declaration
To declare any variable in terraform, use "variable" key word before variable name with a pair of curly braces.
Below are the arguments which we can use for it to define its properties and other stuffs. Though all of these are optional. We can use them as per our need.
1. default - This is to provide a default value for the variable.
2. type - This is to specify the variable's data type.
3. description - This is used to provide a description of variable for better understanding of code.
4. validation - This block is used to do a certain type of validation against the variable value.
5. sensitive - If this is set to true, you won't see these values in output console and these values won't be captured in Log files.
2. type - This is to specify the variable's data type.
3. description - This is used to provide a description of variable for better understanding of code.
4. validation - This block is used to do a certain type of validation against the variable value.
5. sensitive - If this is set to true, you won't see these values in output console and these values won't be captured in Log files.
Example 01:
variable "sa_name" {}
Example 02:
variable "sa_name" { description = "Name of the storage account" type = string default = "bctstsa01" validation { condition = ( length(var.sa_name) >= 3 && length(var.sa_name) <= 24 && can(regex("^[a-z0-9]*$", var.sa_name)) ) error_message = "Storage account name can only consist of lowercase letters and numbers, and must be between 3 and 24 characters long." }}
Variable's Data Type
Below data types are supported in Terraform which are categorised under three categories.
1. Primitive Type
These are not made from any other data type.
- number: a numeric value. integer and float values (ex: 4, 3.41) both can be represented under this.
- string: a sequence of Unicode characters (ex: terraform).
- bool: either true or false. Bools are represented by the unquoted symbols.
Note: The null value is represented by the unquoted symbol null.
2. Complex Type
In which multiple values are grouped into a single value. Complex types are represented by type constructors, but several of them also have shorthand keyword versions.
There are two categories of complex types: collection types (for grouping similar values), and structural types (for grouping potentially dissimilar values).
2.1 Collection Type
2.1.1 List
- Collection of similar type of values. Sequence will start from 0 to access these values.
- Lists can be represented by a pair of square braces [ ]. Commas "," are used to separate the list values. A comma after the final value is allowed, but not required.
Syntax
1. list / list(any) : collection of any element type as long as every element is of the same type.
2. list(string) : list of strings
3. list(number) : list of numbers
Example:# Variable Declarationvariable "test" { type = list(any) default = ["ABC", "xyz", "PQR"]}
# Variable Definition
test = [1, 2, 3]
2.1.2 Map
- Collection of key value pairs, where keys must of string type. A map could only contain values of the same type.
- Maps are represented by a pair of curly braces {} and colons : or equals signs = are used to separate the key value pairs.
- Quotes may be omitted on keys, unless the key starts with a number, in which case quotes are required.
- Key/value pairs can be separated by either a comma or a line break.
Syntax
1. map / map(any) : collection of any element type as long as every element is of the same type.
2. map(string) : map of strings values.
Example 01:# Variable Declarationvariable "test" { type = map(string)}
# Variable Definitiontest = { "name"="abc", "company"="xyz" }
Example 02:# Variable Declarationvariable "test" { type = map(any)}
# Variable Definitiontest = {
"name" = 1
"company" = 2
}
Example 03:# Variable Declarationvariable "test" { type = list(map(string)) default = []}
2.1.3 Set
- A collection of unique values that do not have any ordering.
- The Terraform language doesn't have a literal syntax for set values, but you can use the toset function to explicitly convert a list of strings to a set.
Note
1. A literal expression(*1) is an expression that directly represents a particular constant value.2. When a list or tuple is converted to a set, duplicate values are discarded and the ordering of elements is lost.
3. When a set is converted to a list or tuple, the elements will be in an arbitrary order. If the set's elements were strings, they will be in lexicographical order; sets of other element types do not guarantee any particular order of elements.
2.2 Structural Type
2.2.1 Tuple
- Similar to lists, but each elements can have its own type which means, elements can be of different types.
- Values that match the tuple type must have exactly the same number of elements (no more and no fewer), and the value in each position must match the specified type for that position.
Syntax
tuple([<TYPE>, <TYPE>, ...])
Example:# Variable Declaration
variable "test" { type = tuple([string, number, bool])}
# Variable Definitiontest = ["abc", 5, true]
2.2.2 Object
- Similar to maps but each pair can have different type of values.
- Values that match the object type must contain all of the specified keys, and the value for each key must match its specified type. (Values with additional keys can still match an object type, but the extra attributes are discarded during type conversion.)
Syntax
object({ <KEY> = <TYPE>, <KEY> = <TYPE>, ... })
Example 01:# Variable Declarationvariable "test" { type = object({"name": string, "instances": number})}
# Variable Definitiontest = {"name"="test", "instances"=2}
Example 02:# Variable Declarationvariable "test" {
type = map(list(object({ name = string, cidr = string })))
default = {}
}