简体   繁体   中英

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. 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.

Such feature is not directly supported in TF. But you can force TF to error out using locals and some condition that will simply lead to error if your verification fails. For example, in your test_beanstalk you can have:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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