Terraform Enum data type

April 12, 2022 

In Terraform you have access to basic data types like bool or string. Defining the data type is a good start for starting to improve the quality of your modules. However, you may want to validate that a certain string matches a list of pre-defined options, and if not, fail validation early.

Terraform, unlike Puppet, does not have a built-in Enum data type. However, you can emulate an Enum like this:

variable "type" {
  type = string

  validation {
    condition     = length(regexall("^(public|private)$", var.type)) > 0
    error_message = "ERROR: Valid types are \"public\" and \"private\"!"

This looks crude, but it does work.

Samuli Seppänen
Samuli Seppänen
Author archive