How to Use Optional Arguments in Terraform Variables
In this short article, we will take a look at Terraform variables and the optional arguments they use. We will explain what they are, how to reference them, explain why they are useful, and how to use type constraints to enhance the quality of your code.
What are optional arguments for variable declarations?
When you define a variable in Terraform, it can simply be defined as per the example below:
variable "example_var" {
}However, several optional arguments are available.
These are:
- type
- default
- description
- validation
- sensitive
- nullable
An example variable declaration could look like this with all the optional parameters included:
variable "example_var" {
type = string
default = "chewbacca"
description = "8 foot tall wookie"
validation = {
condition = var.example_var = "chewbacca"
error_message = "Value must be a wookie"
}
sensitive = false
nullable = false
}Let's look at each in turn.
How to use optional arguments — examples
type
Specifies the expected data type of the variable. This helps Terraform validate that the value provided for the variable matches the expected type.
variable "example_var" {
type = string
}default
Provides a default value for the variable. If no value is explicitly passed when applying the configuration, Terraform will use the default value.
variable "example_var" {
type = string
default = "default_value"
}description
Allows you to add a human-readable description to the variable. This description can help other users understand the purpose of the variable. As a suggested good practice you can use the same description as the Terraform docs. For example the name variable description from the Azure Virtual Network page:
variable "name" {
type = string
description = "(Required) The name of the virtual network. Changing this forces a new resource to be created.."
}validation
Enables you to specify a validation rule using a function that the value of the variable must pass. If the validation fails, Terraform will raise an error.
variable "example_var" {
type = number
validation {
condition = var.example_var > 0
error_message = "Value must be greater than 0."
}
}sensitive
When set to true, the value of the variable is treated as sensitive information. Terraform will take care to prevent the value from being shown in logs and outputs. Note that Terraform will still record sensitive values in the state, so anyone who can access the state data will have access to the sensitive values in cleartext.
variable "password" {
type = string
sensitive = true
}nullable
The nullable argument in a variable block controls whether the module caller may assign the value null to the variable. A nullvalue is one that represents absence or omission. If you set an argument of a resource to null, Terraform behaves as though you had completely omitted it — it will use the argument's default value if it has one, or raise an error if the argument is mandatory.
variable "example" {
type = string
nullable = false
}Why are the optional parameters useful?
Using optional parameters or combinations of the optional parameters in your variables can be useful depending on your use case. They are used to further define variables' behavior and characteristics, and make your variables more structured and controlled, making your Terraform configurations more robust and user-friendly.
Type Constraints
Using the type constraint when defining a variable specifies the expected data type of the variable. If no type constraint is set then a value of any type is accepted. If both the type and default arguments are specified, the given default value must be convertible to the specified type. The Terraform language will automatically convert number and bool values to string values when needed, and vice-versa as long as the string contains a valid representation of a number or boolean value.
variable "example_var" {
type = number
}Acceptable ‘primitive’ type values are:
- string
- bool
- number
- any (indicates a value of any type is accepted).
The type constructors also allow you to specify complex types such as collections:
Key points
Variables can be specified with optional parameters to enhance the quality of your code. Including appropriate optional parameters in variables depending on your use case can be useful. As a best practice, you should consider at least including the description to help with the readability of your code, where you may consider adding other parameters unnecessary.
Check out my other articles on Terraform here!
Cheers! 🍻
Originally published at spacelift.io.






