title: How do I include/exclude optional properties within a resource block using Terraform >= 0.12
body: <p>I am pretty new to Terraform, but I am trying to do some conditional logic to build up a resource block.</p>
  <p>So far my naive attempt looks like this, which hopefully gives you an idea of my intent:</p>
  <pre><code>resource &quot;azurerm_kubernetes_cluster_node_pool&quot; &quot;node_pools&quot; {
    { 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        = &quot;${data.azurerm_virtual_network.vnet.id}/subnets/${pool.vnet_subnet_name}&quot;
      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        = &quot;${data.azurerm_virtual_network.vnet.id}/subnets/${pool.vnet_subnet_name}&quot;
      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
    }
  }
  </code></pre>
  <p>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.</p>
  <p>Without conditional logic it might look something like this when enable_auto_scaling == true :</p>
  <pre><code>resource &quot;azurerm_kubernetes_cluster_node_pool&quot; &quot;node_pools&quot; {
    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        = &quot;${data.azurerm_virtual_network.vnet.id}/subnets/${var.node_pools[count.index].vnet_subnet_name}&quot;
    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
  }
  </code></pre>
  <p>And when enable_auto_scaling == false :</p>
  <pre><code>resource &quot;azurerm_kubernetes_cluster_node_pool&quot; &quot;node_pools&quot; {
    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        = &quot;${data.azurerm_virtual_network.vnet.id}/subnets/${var.node_pools[count.index].vnet_subnet_name}&quot;
    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
  }
  </code></pre>
  <p>My variables.tf file contains a variable defined like this:</p>
  <pre><code>variable &quot;node_pools&quot; {
    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
    }))
  }
  </code></pre>
  <p>I am having trouble building a syntactically correct resource block.</p>
  <p>How would you solve this problem?</p>
  <p>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.</p>