I have a TF variable:
variable "test" {
type = number
default = 1
}
I want Terraform to use the specified default value 1
when I pass the TF_VAR_test
variable with an empty value.
Trying this way
TF_VAR_test= terraform plan
fails with
│ Error: Invalid value for input variable
│
│ The environment variable TF_VAR_test does not contain a valid
│ value for variable "test": a number is required.
How can I use the default value if the environment variable exists and is empty?
In that case you can't use a type. Empty string
is not a number
. Instead, you can do the following:
variable "test" {
default = 1
validation {
condition = var.test != "" && can(tonumber(var.test))
error_message = "Only number of emptry string are accepted."
}
}
locals {
test_value = coalesce(tonumber(var.test), 1)
}
And then you use local.test_value
later on.
use local and if statement
variable "test" {
type = string
default = "defaultValue"
}
locals {
test = var.test != "" ? var.test : "defaultValue"
}
output "name" {
value = local.test
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.