简体   繁体   English

Terraform - 确保根据是否还设置了另一个值来设置值

[英]Terraform - ensure value is set depending on if another value is also set

I'd like to enforce that a value is set rather than using the default "" if one of the other values is a certain string.如果其他值之一是某个字符串,我想强制设置一个值而不是使用默认值“”。

For example I have:例如我有:

module "test_beanstalk" {
  tier = "Worker"
  queue = "myQueue"
///
}

in this, when tier is set to worker I'd like to enforce that queue is also set.在此,当tier设置为worker时,我想强制设置该queue In the above example there's a scenario where the queue can be omitted resulting in aws spawning a generic one rather than using the queue that is required for that particular application.在上面的示例中,有一个场景可以省略队列,导致 aws 生成一个通用队列,而不是使用该特定应用程序所需的队列。

Such feature is not directly supported in TF. TF不直接支持此类功能。 But you can force TF to error out using locals and some condition that will simply lead to error if your verification fails.但是您可以强制 TF 使用locals和某些条件强制 TF 出错,如果您的验证失败,这些条件只会导致错误。 For example, in your test_beanstalk you can have:例如,在您的test_beanstalk中,您可以拥有:

variable "tier" {
  default = "Worker"
}

variable "queue" {
  default = ""
}

locals {
  if_queue_given = var.tier == "Worker" && var.queue == "" ? tonumber("queue can't be empty") : 1
}

The tonumber("queue can't be empty") will be executed and will lead to TF error, if the condition var.tier == "Worker" && var.queue == "" is true .如果条件var.tier == "Worker" && var.queue == ""truetonumber("queue can't be empty")将被执行并导致 TF 错误。

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

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