How can I use mutiple count conditions in a module?

I have two booleans, use bastion and use_vault. How can I use both as a condtition in a module, i.e. when both true do?

variable "use_bastion" {
  type        = bool
  default     = true
}

variable "use_vault" {
  type        = bool
  default     = true
}

I’m using count and a single check is fine, i.e.

 count  = var.use_vault ? 1 : 0

I tried combining as follows

 count = "${var.use_vault * var.use_bastion}"

but it fails with
“var.use_vault is a bool, known only after apply. Unsuitable value for left operand: number required.”

Nested doesn’t fail but doesn’t give me what I need, i.e.

 count = ( var.use_vault ? 1 : ( var.use_bastion ? 1 : 0 ))

Hi @mick_a_thompson!

Terraform returned that error because * is an operator for numbers (multiplication), and so it doesn’t make sense to apply it to boolean values.

It sounds like your goal is to declare a resource instance only if var.use_vault and var.use_bastion are both set. If so, you can use the “logical AND” operator && to represent that:

  count = var.use_vault && var.use_bastion ? 1 : 0

This is one of the logical operators. You can alternatively use || (logical OR) if your goal was to declare one object if either variable is set.

2 Likes

Ah yeah, that makes sence. Sorted. Thank you!

Hi @apparentlymart, if we have two different type of variables, how we can use count in that case?

@pratiksharathod298,

Please start a new topic and explain fully what you’re trying to do. For more information on what will make your question answerable, refer to Guide to asking for help in this forum.