Terraform Conditionals: Handling Non-Existent Variables
TLDR
To handle non-existent variables in Terraform, use the lookup
function or the ternary conditional operator. These techniques allow you to provide default values or handle undefined variables gracefully.
Terraform configurations often rely on variables to define dynamic values. However, there are cases where a variable might not be defined. This guide will show you how to handle such scenarios using Terraform conditionals.
Why Handle Non-Existent Variables?
- Flexibility: Allow configurations to work even when some variables are not defined.
- Default Values: Provide sensible defaults for optional variables.
- Error Prevention: Avoid runtime errors caused by missing variables.
Using the lookup Function
The lookup
function retrieves a value from a map and allows you to specify a default value if the key does not exist.
Example: Using lookup
variable "config" {
type = map(string)
}
output "example" {
value = lookup(var.config, "key", "default-value")
}
In this example, if var.config
does not contain the key key
, the output will use default-value
.
Using the Ternary Conditional Operator
The ternary operator allows you to check if a variable is defined and provide a fallback value.
Example: Using the Ternary Operator
variable "optional_var" {
default = null
}
output "example" {
value = var.optional_var != null ? var.optional_var : "default-value"
}
In this example, if var.optional_var
is not defined, the output will use default-value
.
Best Practices
- Use Defaults: Define default values for variables whenever possible.
- Validate Inputs: Use
validation
blocks to enforce constraints on variable values. - Document Variables: Clearly document which variables are optional and their default values.
By using these techniques, you can handle non-existent variables in Terraform gracefully, making your configurations more robust and flexible.
Found an issue?