Okay, I understand the issue. Terraform's expression evaluation can sometimes try to evaluate both sides of an && or || operation conceptually or for type checking before determining the final result, leading to errors like "argument must not be null" when an optional value is null and used in an operation that doesn't support null (like numerical comparison).
The validation condition secret.rotation_period == null || (secret.rotation_period >= 3600 && secret.rotation_period <= 3153600000) is the problem. While the || is supposed to short-circuit, the comparison part (secret.rotation_period >= 3600 && ...) still causes an error when secret.rotation_period is null.
A safer way to write this condition in Terraform is to use an if expression or the can() function, ensuring that the numerical comparison is only attempted if the value is not null.
Here's the corrected validation block using an if expression:
variable "secrets" {
description = "List of secrets to create for this application"
type = list(object({
name = string
application_module = optional(string, "-")
replication_regions = optional(list(string))
rotation_period = optional(number)
rotation_topics = optional(list(string))
expire_time = optional(string)
delay_destroy = optional(number)
}))
validation {
condition = alltrue([
for secret in var.secrets : secret.name != null && secret.name != ""
])
error_message = "Each secret must have a non-empty name."
}
validation {
# Use an if expression to only perform the comparison if rotation_period is not null
condition = alltrue([
for secret in var.secrets :
secret.rotation_period == null ? true : (secret.rotation_period >= 3600 && secret.rotation_period <= 3153600000)
])
error_message = "If specified, rotation_period must be between 3600 and 3153600000 seconds."
}
validation {
condition = alltrue([
for secret in var.secrets : secret.rotation_topics == null || length(coalesce(secret.rotation_topics, [])) <= 10
])
error_message = "Maximum 10 rotation topics can be specified per secret."
}
validation {
condition = alltrue([
for secret in var.secrets : secret.expire_time == null || can(regex("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$", secret.expire_time))
])
error_message = "Expire time must be a valid RFC3339 UTC 'Zulu' format timestamp, e.g., '2023-12-31T23:59:59Z'."
}
}
Explanation of the fix:
The problematic line:
secret.rotation_period == null || (secret.rotation_period >= 3600 && secret.rotation_period <= 3153600000)
Is replaced with: