Test number between range

Hello all,
I can’t seem to find the correct way to do this in TF. But i’m trying to test if an input value is between 2 numbers. This is what I’ve tried (and failed), but can’t really figure out if there’s a test option for number ranges.

variable "kms_data_key_reuse_period_seconds" {
  description = "The length of time, in seconds, for which Amazon SQS can reuse a data key to encrypt or decrypt messages before calling AWS KMS again. An integer representing seconds, between 60 seconds (1 minute) and 86,400 seconds (24 hours). The default is 300 (5 minutes)."
type = number
default = 60
validation {
  condition = can(regex(var.kms_data_key_reuse_period_seconds, range(60, 86400)))
  error_message = "Invalid Range: 60 - 86400."

Hi @djtecha,

You can write a condition about the range of a number using the comparison operators:

  validation {
    condition = (
      var.kms_data_key_reuse_period_seconds >= 60 &&
      var.kms_data_key_reuse_period_seconds <= 86400
    error_message = "Must be between 60 and 86400 seconds, inclusive."

The regex function is for string matching, so it’s not a useful function to use with numbers.

haha ohh yea… Thanks was having one of those days. So obvious now :slight_smile: