Terraform - Specifying multiple possible values for Variables

微笑、不失礼 提交于 2019-12-11 01:19:24

问题


CloudFormation provides AllowedValues for Parameters which tells that the possible value of the parameter can be from this list. How can I achieve this with Terraform variables? The variable type of list does not provide this functionality. So, in case I want my variable to have value out of only two possible values, how can I achieve this with Terraform. CloudFormation script that I want to replicate is:

"ParameterName": {
        "Description": "desc",
        "Type": "String",
        "Default": true,
        "AllowedValues": [
            "true",
            "false"
        ]
   }

回答1:


I don't know of an official way, but there's an interesting technique described in a Terraform issue:

variable "values_list" {
  description = "acceptable values"
  type = "list"
  default = ["true", "false"]
}

variable "somevar" {
description = "must be true or false"
}

resource "null_resource" "is_variable_value_valid" {
  count = "${contains(var.values_list, var.somevar) == true ? 0 : 1}"
  "ERROR: The somevar value can only be: true or false" = true
}


来源:https://stackoverflow.com/questions/54254524/terraform-specifying-multiple-possible-values-for-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!