繁体   English   中英

用 Terraform 中的用户输入来计数?

[英]count with an input from user in Terraform?

我想根据用户输入的数字使用 terraform 创建许多资源。 数字必须在 2 到 5 之间。我试过:在vars.tf

variable "user_count" {
    type = number
    default = 2
    description = "How many number of VMs to create (minimum 2 and no more than 5): "
}

这里的问题是它使用默认编号 2 创建资源。

另一个案例:

variable "user_count" {
        type = number
        description = "How many number of VMs to create (minimum 2 and no more than 5): "
    }

这里,没有默认参数。 我收到消息/描述,但我/用户可以输入任何内容! 如何使这成为可能? - 用户收到消息并验证数字在 2 到 5 之间,否则不会创建资源。

感谢任何帮助 - 我真的被困在其中!

您可以尝试自定义验证

variable "user_count" {
        type = number
        description = "How many number of VMs to create (minimum 2 and no more than 5): "
        validation {
          condition     = var.user_count > 1 && var.user_count < 6
          error_message = "Validation condition of the user_count variable did not meet."
        }
}

但也许更好的选择而不是检查数字,将作为字符串和正则表达式变量来检查值是 2、3、4 还是 5。

variable "user_count" {
  type        = string
  description = "How many number of VMs to create (minimum 2 and no more than 5): "

  validation {
    # regex(...) fails if it cannot find a match
    condition     = can(regex("2|3|4|5", var.user_count))
    error_message = "Validation condition of the user_count variable did not meet."
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM