Tomer Leibovich
Tomer Leibovich

Reputation: 372

How to conditional create resource in Terraform based on a string variable

While it is common to create a resource based on a boolean variable conditionally, I'm looking for a way to conditionally generate the resource base on the string in the variable.

For example, I am creating the variable day = Sunday; now, if the variable is not Sunday, Terraform will create the resource; else - nothing will get created.

Is there a way in TF to achieve that?

Upvotes: 6

Views: 12164

Answers (1)

harshavmb
harshavmb

Reputation: 3872

If I understand correctly, you just replace the boolean condition with string condition?

For example::

variable "day" {
  type        = string
  default     = "Sunday"
  description = "Defaults to Sunday. We only create resource when it's not Sunday"
}

Then the resource, could be created when it's not Sunday::

resource "some_resource" { 
  count = var.day != "Sunday" ? 1 : 0 
  ... 
}

If this is not you are expecting, then my understanding is incorrect. Spare me for that..

Upvotes: 9

Related Questions