Limit inputs to only allow certain pre-defined strings, similar to Puppet's enum type?

I want to limit inputs on a variable to only one of a handful of strings.

I am writing a module to create some resources in Azure, and need to limit the input to prevent arbitrary values from being used so that terraform throws an error if a value that isn’t allowed is used when defining the module parameters.

Hi @tspearconquest,

In Terraform a constraint like that would be a validation rule rather than a type constraint, but you can indeed achieve that result using a validation block in the definition of your variable:

variable "example" {
  type = string

  validation {
    condition     = contains(["a", "b"], var.example)
    error_message = "Must be either \"a\" or \"b\"."
  }
}

Hi, thank you!

Is there any way to do something like the above while also basing part of the condition on another variable’s value? I tried but got an error about using another variable in the condition.

Something similar to below:

variable "foo" {
  type      = string
  default = "bar"
}

variable "bee" {
  type = string

  validation {
    condition     = length(var.bee) > 0 && length(var.bee) < 5 && var.bee != var.foo
    error_message = "Must not match other variable exactly"
  }
}

Thanks in advance