7

I'm trying to use Terraform 0.13's custom variable validation to ensure that the value is either null or between a specified range. I've tried every combination I can think of (using can, try, etc) but can't get it working for every case.

Here is the full non-working example:

variable "target_group_stickiness_duration" {
  default = null
  type    = number
 
  validation {
    condition = ( 
      var.target_group_stickiness_duration == null ||
      ( var.target_group_stickiness_duration >= 1 && var.target_group_stickiness_duration <= 604800 )
    )   
 
    error_message = "Target group stickiness duration must be >= 1 and <= 604800."
  }
}

This will fail if the value is null with the following error:

Error during operation: argument must not be null.

I believe this is due to eager evaluation of the conditional expression. Am I missing something obvious? Or any clever workarounds for this?

2 Answers 2

16

It looks like there are two different things going on here.

The first is that your condition expression includes the following subexpression:

var.target_group_stickiness_duration == null

This seems to be allowing the value to be null.

However, if we correct that to the opposite then it will expose a second problem:

    condition = ( 
      var.target_group_stickiness_duration != null &&
      ( var.target_group_stickiness_duration >= 1 && var.target_group_stickiness_duration <= 604800 )
    )   

Terraform's logical operators don't have the "short circuit" behavior in some other languages, where the result of the first operand can prevent any attempt to evaluate the second operand. Therefore both operands of && must be valid and successfully return either true or false.

The second operand (the nested && testing the range of the number) can't succeed if the value is null, because >= isn't defined for null numbers.

To make this work requires using a different strategy that avoids applying the >= operator to a null number. There are a few different ways to do that, including one with coalesce discussed in the accepted answer to this question.

Another alternative strategy is to use the conditional operator, which does allow the unselected result operand to fail, to allow us to use it as a guard for situations like this:

  condition = (
    var.target_group_stickiness_duration == null ? false : (
      var.target_group_stickiness_duration >= 1 && 
      var.target_group_stickiness_duration <= 604800
    )
  )

This is essentially an explicit implementation of the automatic short-circuit behavior some other languages offer in their logical operators. Terraform, at least in its current design, requires you to explicitly write out that the >= and <= operations are desirable only for non-null numbers.

3
  • This makes sense and I tried this version too, but it fails with the same error as above. The second conditional expression is always getting evaluated with the null value.
    – richid
    Apr 2, 2021 at 3:26
  • 2
    Ahh yes, with fresh eyes today I see that there were two different things going on here and I focused only one the first one I saw. I'll update my answer to be more complete, although I see you already accepted the other answer so I'm just doing this so as not to leave an incomplete answer here. Apr 2, 2021 at 22:54
  • 3
    One minor change: the first part of the ternary should be true, but this is a very clean solution. Major thanks for pointing out that the conditional/ternary operator supports short-circuiting, that'll help in the future!
    – richid
    Apr 5, 2021 at 15:13
9

It fails becasue when your target_group_stickiness_duration is null, the

(var.target_group_stickiness_duration >= 1 && var.target_group_stickiness_duration <= 604800
)

is invalid.

You can try the following which uses coalesce:

variable "target_group_stickiness_duration" {
  default = null
  type    = number
 
  validation {
    condition = ( 
      var.target_group_stickiness_duration == null ||      
     (coalesce(var.target_group_stickiness_duration, 0) >= 1 && coalesce(var.target_group_stickiness_duration, 604801) <= 604800))
 
    error_message = "Target group stickiness duration must be >= 1 and <= 604800."
  }
  
}
1
  • 1
    Tried about 10 different things but didn't think to use coalesce. Thanks so much!
    – richid
    Apr 2, 2021 at 3:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.