1
votes

I am pretty new to Terraform, but I am trying to do some conditional logic to build up a resource block.

So far my naive attempt looks like this, which hopefully gives you an idea of my intent:

resource "azurerm_kubernetes_cluster_node_pool" "node_pools" {
  { for pool in var.node_pools:
    kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
    name                  = pool.name
    vm_size               = pool.vm_size
    max_pods              = pool.max_pods
    vnet_subnet_id        = "${data.azurerm_virtual_network.vnet.id}/subnets/${pool.vnet_subnet_name}"
    os_type               = pool.os_type
    os_disk_size_gb       = pool.os_disk_size_gb
    node_taints           = pool.node_taints
    availability_zones    = pool.availability_zones
    node_labels           = pool.node_labels
    enable_auto_scaling   = pool.enable_auto_scaling
    max_count             = pool.max_count
    min_count             = pool.min_count
    if pool.enable_auto_scaling
  }
    { for pool in var.node_pools:
    kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
    name                  = pool.name
    vm_size               = pool.vm_size
    max_pods              = pool.max_pods
    vnet_subnet_id        = "${data.azurerm_virtual_network.vnet.id}/subnets/${pool.vnet_subnet_name}"
    os_type               = pool.os_type
    os_disk_size_gb       = pool.os_disk_size_gb
    node_taints           = pool.node_taints
    availability_zones    = pool.availability_zones
    node_labels           = pool.node_labels
    enable_auto_scaling   = pool.enable_auto_scaling
    node_count            = pool.count
    if !pool.enable_auto_scaling
  }
}

If pool.enable_auto_scaling is true, I want to have max_count and min_count properties. If its false, I just want node_count.

Without conditional logic it might look something like this when enable_auto_scaling == true :

resource "azurerm_kubernetes_cluster_node_pool" "node_pools" {
  count                 = length(var.node_pools)
  kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
  name                  = var.node_pools[count.index].name
  vm_size               = var.node_pools[count.index].vm_size
  max_pods              = var.node_pools[count.index].max_pods
  vnet_subnet_id        = "${data.azurerm_virtual_network.vnet.id}/subnets/${var.node_pools[count.index].vnet_subnet_name}"
  os_type               = var.node_pools[count.index].os_type
  os_disk_size_gb       = var.node_pools[count.index].os_disk_size_gb
  node_taints           = var.node_pools[count.index].node_taints
  availability_zones    = var.node_pools[count.index].availability_zones
  node_labels           = var.node_pools[count.index].node_labels
  enable_auto_scaling   = var.node_pools[count.index].enable_auto_scaling
  max_count             = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].max_count : 0
  min_count             = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].min_count : 0
}

And when enable_auto_scaling == false :

resource "azurerm_kubernetes_cluster_node_pool" "node_pools" {
  count                 = length(var.node_pools)
  kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
  name                  = var.node_pools[count.index].name
  vm_size               = var.node_pools[count.index].vm_size
  max_pods              = var.node_pools[count.index].max_pods
  vnet_subnet_id        = "${data.azurerm_virtual_network.vnet.id}/subnets/${var.node_pools[count.index].vnet_subnet_name}"
  os_type               = var.node_pools[count.index].os_type
  os_disk_size_gb       = var.node_pools[count.index].os_disk_size_gb
  node_taints           = var.node_pools[count.index].node_taints
  availability_zones    = var.node_pools[count.index].availability_zones
  node_labels           = var.node_pools[count.index].node_labels
  enable_auto_scaling   = var.node_pools[count.index].enable_auto_scaling
  node_count            = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].min_count : var.node_pools[count.index].count
}

My variables.tf file contains a variable defined like this:

variable "node_pools" {
  type = list(object({
    name                = string
    count               = number
    vm_size             = string
    max_pods            = number
    vnet_subnet_name    = string
    os_type             = string
    os_disk_size_gb     = number
    node_taints         = list(string)
    availability_zones  = list(string)
    node_labels         = map(string)
    enable_auto_scaling = bool
    max_count           = number
    min_count           = number
  }))
}

I am having trouble building a syntactically correct resource block.

How would you solve this problem?

Setting max_count / min_count / node_count to zero or null does not work, Azure RM API will return errors if the properties are present, it seems the only way it works is to exclude the properties from the resource block entirely.

2

2 Answers

2
votes

For arguments for resources in resource blocks, omitting an argument and setting it to null are equivalent, so if you need to "conditionally omit" an argument then you can restate the problem as conditionally setting it to null:

var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].min_count : null

This is different than having the false result be 0, because null allows the provider to decide what value to use for that argument, while 0 forces it to be set to zero (and so Terraform will consider a future nonzero value to be drift and plan to change it back again).

1
votes

Your azurerm_kubernetes_cluster_node_pool can have all of min_count, max_count, and node_count present. When you want to leave them unset, use null, which is interpreted by the provider as if they were not declared in the resource block at all.

The specific arguments that need fixing are these:

max_count  = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].max_count : null
min_count  = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].min_count : null
node_count = var.node_pools[count.index].enable_auto_scaling ? null: var.node_pools[count.index].count
  • max_count and min_count default to null if enable_auto_scaling is false
  • node_count defaults to null if enable_auto_scaling is true

In context, this would look like:

resource "azurerm_kubernetes_cluster_node_pool" "node_pools" {
  count                 = length(var.node_pools)
  kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
  name                  = var.node_pools[count.index].name
  vm_size               = var.node_pools[count.index].vm_size
  max_pods              = var.node_pools[count.index].max_pods
  vnet_subnet_id        = "${data.azurerm_virtual_network.vnet.id}/subnets/${var.node_pools[count.index].vnet_subnet_name}"
  os_type               = var.node_pools[count.index].os_type
  os_disk_size_gb       = var.node_pools[count.index].os_disk_size_gb
  node_taints           = var.node_pools[count.index].node_taints
  availability_zones    = var.node_pools[count.index].availability_zones
  node_labels           = var.node_pools[count.index].node_labels
  enable_auto_scaling   = var.node_pools[count.index].enable_auto_scaling
  max_count             = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].max_count : null
  min_count             = var.node_pools[count.index].enable_auto_scaling ? var.node_pools[count.index].min_count : null
  node_count            = var.node_pools[count.index].enable_auto_scaling ? null: var.node_pools[count.index].count
}