steve
steve

Reputation: 113

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.

Upvotes: 3

Views: 472

Answers (1)

Marcin
Marcin

Reputation: 238957

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.

Upvotes: 5

Related Questions