'Variables definition for optional parameters options - Terraform

I'm trying to write code for AKS with all possible arguments, and then in variables I specify map. Problem is I don't want to specify all of the optional arguments in my variables each time, I wonder if there is a possibility to skip them in variables. Here is my example:

main.tf

resource "azurerm_kubernetes_cluster" "this" {

  for_each = var.AKS_Config

  name                = each.value.AKS_Name
  location            = each.value.AKS_Location
  resource_group_name = each.value.AKS_ResourceGroupName
  dns_prefix          = each.value.dns_prefix
  #dns_prefix_private_cluster = # dns_prefix or dns_prefix_private_cluster must be specified
  kubernetes_version              = each.value.kubernetes_version
  automatic_channel_upgrade       = each.value.automatic_channel_upgrade
  api_server_authorized_ip_ranges = each.value.api_server_authorized_ip_ranges

  dynamic "identity" {
    for_each = each.value.identity
    content {
      type                      = identity.value.type
      user_assigned_identity_id = can(identity.value.user_assigned_identity_id) == true ? identity.value.user_assigned_identity_id : {}
    }
  }
}
variables.tf
variable "AKS_Config" {
  default = {
    "AKS_1" = {
      AKS_Name                        = "AKSTest001"
      AKS_Location                    = "West Europe"
      AKS_ResourceGroupName           = "SDSADAS"
      dns_prefix                      = "AKSTESTPREFIX"
      default_node_pool_name          = "test-name"
      node_count                      = 1
      vm_sku                          = "Standard_D2_v2"
      kubernetes_version              = "1.21.7"
      automatic_channel_upgrade       = "stable"
      api_server_authorized_ip_ranges = ["16.0.0.0/16"]
      identity = {
        type = "SystemAssigned"
      }
      default_node_pool = {
        name                   = "nodetest01"
        node_count             = 3
        vm_size                = "Standard_D2_v2"
        availability_zones     = ["1", "2", "3"]
        auto_scaling_enabled   = true
        enable_host_encryption = false
        enable_node_public_ip  = true
        fips_enabled           = true
        kubelet_disk_type      = "OS"
        max_pods               = 2
      }
    }
  }
}

As you can see there are bunch of optional arguments for AKS, but I don't want to specify each one of them as 'null' every deployment. Is there a option to achieve that? Some kind of function 'if key does not exist skip it'

Thank you

EDIT:

What do you think about this "workaround"?

  dynamic "identity" {
    for_each = can(each.value.identity) == true ? each.value.identity : {}
    content {
      type                      = each.value.identity.type
      user_assigned_identity_id = can(each.value.identity.user_assigned_identity_id) == true ? each.value.identity.user_assigned_identity_id : null
    }
  }

Using can it's working, or at least terraform validate doesn't throw any problems and I don't need to specify optional parameters in map, but I don't know if this should be avoided in my case



Solution 1:[1]

Generally there are two options I see to handle this problem:

  1. Split the default map to different vaiables, and assign to each variable a default value.
  2. Use the merge() terraform function in order to override specific values in the default map you declared to be used in the for_each.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tamir