如何分配默认值映射(对象())输入在Terraform?



定义复杂输入变量时:

variable "s3_shares" {
type = map(object({
s3_bucket_arn         = string
client_list           = list(string)
read_only             = bool
default_storage_class = string
}))
}

如何处理read_onlydefault_storage_class是互斥的?换句话说,在使用该模块并使用read_only = true定义s3_share时,可以省略default_storage_class

使用验证{}块和alltrue()函数:

variable "s3_shares" {
type = map(object({
s3_bucket_arn         = string
client_list           = list(string)
read_only             = bool
default_storage_class = string
}))
default = {
"one" = {
s3_bucket_arn         = "foo"
client_list           = ["foo","bar"]
read_only             = false
default_storage_class = "bar" # IS OK
}
"two" = {
s3_bucket_arn         = "foo"
client_list           = ["foo","bar"]
read_only             = false
default_storage_class = "" # IS OK
}}
"three" = {
s3_bucket_arn         = "foo"
client_list           = ["foo","bar"]
read_only             = true
default_storage_class = "" # IS OK
}}
"four" = {
s3_bucket_arn         = "foo"
client_list           = ["foo","bar"]
read_only             = true
default_storage_class = "bar" # IS KO
}}
validation {
condition = alltrue([
for o in var.s3_shares : !(o.read_only && length(o.default_storage_class) > 0)])
error_message = "Read_only and default_storage_class are exclusive."     
}
}

注意用例"two">其中read_only设置为false,default_storage_class为空:这将返回true。这可能不是您想要的行为。

相关内容

  • 没有找到相关文章

最新更新