我有一个TF变量:
variable "test" {
type = number
default = 1
}
我想要Terraform使用指定的默认值1
时,我传递TF_VAR_test
变量与一个空值。
试着这样做
TF_VAR_test= terraform plan
与失败
│ 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.
如果环境变量存在且为空,如何使用默认值?
在这种情况下,不能使用类型。空的string
不是number
。相反,您可以这样做:
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)
}
然后在后面使用local.test_value
。
使用local and if语句
variable "test" {
type = string
default = "defaultValue"
}
locals {
test = var.test != "" ? var.test : "defaultValue"
}
output "name" {
value = local.test
}