I have the following variable maintenance_window_auto_upgrade
that defaults to {}
. My issue is Terraform throws an error as it expects the non-optional properties duration
, frequency
, and interval
to be supplied. The exact error I get is below. What is the correct way to have the variable be optional while still supporting the required properties when maintenance_window_auto_upgrade
is supplied? Also below is how I expect to use this variable with the azurerm_kubernetes_cluster
resource. Thanks!
“This default value is not compatible with the variable’s type constraint: attributes “duration”, “frequency”, and “interval” are required.”
variable "maintenance_window_auto_upgrade" {
type = object({
day_of_week = optional(string)
duration = number
frequency = string
interval = number
start_date = optional(string)
start_time = optional(string)
utc_offset = optional(string)
week_index = optional(string)
not_allowed = optional(list(object({
end = string
start = string
})), [])
})
default = {}
description = "Use Planned Maintenance to schedule and control upgrades for your Azure Kubernetes Service (AKS) cluster."
validation { # day_of_week
condition = contains(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], var.maintenance_window_auto_upgrade.day_of_week)
error_message = "day_of_week must be one of 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', or 'Sunday'."
}
validation { # week_index
condition = contains(["First", "Second", "Third", "Fourth", "Last"], var.maintenance_window_auto_upgrade.week_index)
error_message = "week_index must be one of 'First', 'Second', 'Third', 'Fourth', or 'Last'."
}
validation { # frequency
condition = contains(["Weekly", "AbsoluteMonthly", "RelativeMonthly"], var.maintenance_window_auto_upgrade.frequency)
error_message = "frequency must be one of 'Weekly', 'AbsoluteMonthly', or 'RelativeMonthly'."
}
validation { # duration
condition = (
var.maintenance_window_auto_upgrade.duration >= 4 &&
var.maintenance_window_auto_upgrade.duration <= 24
)
error_message = "day_of_month must be between 4 and 24."
}
}
// example showing variable usage
resource "azurerm_kubernetes_cluster" "aks" {
name = var.name
.
..
...
dynamic "maintenance_window_auto_upgrade" {
for_each = var.maintenance_window_auto_upgrade != null ? [var.maintenance_window_auto_upgrade] : []
content {
day_of_week = maintenance_window_auto_upgrade.value["day_of_week"]
duration = maintenance_window_auto_upgrade.value["duration"]
frequency = maintenance_window_auto_upgrade.value["frequency"]
interval = maintenance_window_auto_upgrade.value["interval"]
start_date = maintenance_window_auto_upgrade.value["start_date"]
start_time = maintenance_window_auto_upgrade.value["start_time"]
utc_offset = maintenance_window_auto_upgrade.value["utc_offset"]
week_index = maintenance_window_auto_upgrade.value["week_index"]
dynamic "not_allowed" {
for_each = length(maintenance_window_auto_upgrade.value["not_allowed"]) > 0 ? maintenance_window_auto_upgrade.value["not_allowed"] : []
content {
end = not_allowed.value["end"]
start = not_allowed.value["start"]
}
}
}
}