6

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?

2
  • Terraform manages resources, and does not "create or not create". You will probably need to approach this differently, or else the end result will not be what you want. Commented May 2, 2022 at 14:22
  • Thank you for your reply, however, if I'm working with a boolean variable, TF will create/not create the resource: resource "some_resource" { count = var.enable_autoscaling ? 1 : 0 ... } Commented May 2, 2022 at 14:27

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.