12

CloudFormation provides AllowedValues for Parameters which tells that the possible value of the parameter can be from this list. How can I achieve this with Terraform variables? The variable type of list does not provide this functionality. So, in case I want my variable to have value out of only two possible values, how can I achieve this with Terraform. CloudFormation script that I want to replicate is:

"ParameterName": {
        "Description": "desc",
        "Type": "String",
        "Default": true,
        "AllowedValues": [
            "true",
            "false"
        ]
   }

3 Answers 3

17

I don't know of an official way, but there's an interesting technique described in a Terraform issue:

variable "values_list" {
  description = "acceptable values"
  type = "list"
  default = ["true", "false"]
}

variable "somevar" {
description = "must be true or false"
}

resource "null_resource" "is_variable_value_valid" {
  count = "${contains(var.values_list, var.somevar) == true ? 0 : 1}"
  "ERROR: The somevar value can only be: true or false" = true
}

Update:

Terraform now offers custom validation rules in Terraform 0.13:

variable "somevar" {
  type = string
  description = "must be true or false"

  validation {
    condition     = can(regex("^(true|false)$", var.somevar))
    error_message = "Must be true or false."
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

So if the variable is not in the list, Terraform will raise an error and stop further process? If the variable is in the list, the value of count would be zero? And the error will be thrown when count is 1?
17

Custom validation rules is definitely the way to go. If you want to keep things simple and check the provided value against a list of valid ones, you can use the following in your variables.tf config:

variable "environment" {
  type = string
  description = "Deployment environment"
  validation {
    condition = contains(["dev", "prod"], var.environment)
    error_message = "Valid value is one of the following: dev, prod."
  }
}

Comments

1

Variation on the above answer to use an array/list.

variable "appservice_sku" {

    type        = string 
    description = "AppService Plan SKU code"
    default     = "P1v3"

    validation {
        error_message = "Please use a valid AppService SKU."
        condition = can(regex(join("", concat(["^("], [join("|", [ 
            "B1", "B2", "B3", "D1", "F1", 
            "FREE", "I1", "I1v2", "I2", "I2v2", 
            "I3", "I3v2", "P1V2", "P1V3", "P2V2", 
            "P2V3", "P3V2", "P3V3", "PC2", 
            "PC3", "PC4", "S1", "S2", "S3", 
            "SHARED", "WS1", "WS2", "WS3"
        ])], [")$"])), var.appservice_sku))
    }
}

1 Comment

i'm not sure i get this code right, but if I do, isnt contains(['b1','b2',...'ws3'], var.appservice_sku) a lot easier to understand?

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.