@@ -92,6 +92,15 @@ resource "azurerm_kubernetes_cluster" "aks" {
9292 tags = var. tags
9393}
9494
95+ locals {
96+ # Create a map with spot instance flags for each node pool
97+ node_pool_configs = {
98+ for k , v in var . node_pools : k => {
99+ is_spot = lower (v. priority ) == " spot"
100+ }
101+ }
102+ }
103+
95104resource "azurerm_kubernetes_cluster_node_pool" "additional" {
96105 for_each = var. node_pools
97106
@@ -120,9 +129,9 @@ resource "azurerm_kubernetes_cluster_node_pool" "additional" {
120129 ]
121130
122131 # Normalize to the capitalized form expected by the provider
123- priority = lower ( each. value . priority ) == " spot " ? " Spot" : " Regular"
124- eviction_policy = lower ( each. value . priority ) == " spot " ? each. value . eviction_policy : null
125- spot_max_price = lower ( each. value . priority ) == " spot " ? each. value . spot_max_price : null
132+ priority = local . node_pool_configs [ each . key ] . is_spot ? " Spot" : " Regular"
133+ eviction_policy = local . node_pool_configs [ each . key ] . is_spot ? title ( lower ( each. value . eviction_policy )) : null
134+ spot_max_price = local . node_pool_configs [ each . key ] . is_spot ? each. value . spot_max_price : null
126135
127136 tags = merge (var. tags , each. value . tags )
128137
@@ -132,7 +141,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "additional" {
132141 error_message = " Node pool '${ each . key } ' has invalid priority '${ each . value . priority } '. Priority must be either 'Regular' or 'Spot' (case-insensitive)."
133142 }
134143 precondition {
135- condition = lower ( each. value . priority ) != " spot " || contains ([" delete" , " deallocate" ], lower (each. value . eviction_policy ))
144+ condition = ! local . node_pool_configs [ each . key ] . is_spot || contains ([" delete" , " deallocate" ], lower (each. value . eviction_policy ))
136145 error_message = " Node pool '${ each . key } ' has invalid eviction_policy '${ each . value . eviction_policy } '. When priority is 'Spot', eviction_policy must be either 'Delete' or 'Deallocate' (case-insensitive)."
137146 }
138147 precondition {
0 commit comments