有什么方法可以在 terraform 中定义允许的值吗?

Is there any way to define allowed values in terraform?

我想创建一个 Azure Redis 缓存,并希望从不同的 SKU 中为 select 提供选项。不支持允许值参数,因此我不能提及不同的 SKU。有没有办法提一下?

如您所知,到目前为止,terraform 不支持 allowed values 参数。

如果你想在输入变量时提及允许的值,你可以使用这样的变量描述,

variable "SKU" {
  description = "which SKU do you want (options: Basic,Standard,Premium)"
  type = "string"
}

或者,作为 Github 中 this issue 的解决方法。您可以使用本地地图和键查找并添加值检查器。

variable "sku" {
  description = "which SKU do you want (options: Basic,Standard,Premium)"
  type = "string"

}

locals {
  sku_options = ["Basic","Standard","Premium" ]
 # or add this to precisely match the value that case sensitive, validate_sku = "${index(local.sku_options, var.sku)}"
}

resource "null_resource" "is_sku_name_valid" {
  count = "${contains(local.sku_options, var.sku) == true ? 0 : 1 }"

}

希望对您有所帮助。

这将在 Terraform 0.13 中可用。对于您的特定用例,这将如下所示:

variable "sku" {
  description = "which SKU do you want (options: Basic,Standard,Premium)"
  type = "string"
  validation {
    condition     = contains(["Basic", "Standard", "Premium"], var.sku)
    error_message = "Argument 'sku' must one of 'Basic', 'Standard', or 'Premium'."
  }
}