<p>I have a <strong>module</strong> definition as below:</p>
<p>===</p>
<p><strong>providers.tf</strong></p>
<pre><code>provider &quot;kubernetes&quot; {
  #load_config_file = &quot;false&quot;
  host                   = azurerm_kubernetes_cluster.aks.kube_config.0.host
  username               = azurerm_kubernetes_cluster.aks.kube_config.0.username
  password               = azurerm_kubernetes_cluster.aks.kube_config.0.password
  client_certificate     = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
  client_key             = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
  cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}
</code></pre>
<p><strong>outputs.tf</strong></p>
<pre><code>output &quot;node_resource_group&quot; {
  value       = azurerm_kubernetes_cluster.aks.node_resource_group
  description = &quot;The name of resource group where the AKS Nodes are created&quot;
}
output &quot;kubeConfig&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config_raw
  description = &quot;Kubeconfig of AKS Cluster&quot;
}

output &quot;host&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config.0.host
}

output &quot;client_key&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config.0.client_key
}

output &quot;client_certificate&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate
}

output &quot;kube_config&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config_raw
}

output &quot;cluster_ca_certificate&quot; {
  value = azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate
}
</code></pre>
<p><strong>main.tf</strong></p>
<pre><code>resource &quot;azurerm_log_analytics_workspace&quot; &quot;law&quot; {
  name                = &quot;${var.tla}-la-${local.lookup_result}-${var.identifier}&quot;
  location            = data.azurerm_resource_group.rg.location
  resource_group_name = data.azurerm_resource_group.rg.name
  sku                 = var.la_sku
  retention_in_days   = 30
}

resource &quot;azurerm_kubernetes_cluster&quot; &quot;aks&quot; {
  name                    = &quot;${var.tla}-aks-${local.lookup_result}-${var.identifier}&quot;
  location                = data.azurerm_resource_group.rg.location
  resource_group_name     = data.azurerm_resource_group.rg.name
  dns_prefix              = var.dns_prefix
  kubernetes_version      = var.kubernetes_version
  sku_tier                = var.sku_tier
  private_cluster_enabled = var.enable_private_cluster
  #api_server_authorized_ip_ranges = &quot;&quot;
  default_node_pool {
    name                  = &quot;syspool001&quot;
    orchestrator_version  = var.orchestrator_version
    availability_zones    = var.agents_availability_zones
    enable_auto_scaling   = true
    node_count            = var.default_pool_node_count
    max_count             = var.default_pool_max_node_count
    min_count             = var.default_pool_min_node_count
    max_pods              = var.default_pool_max_pod_count
    vm_size               = var.agents_size
    enable_node_public_ip = false
    os_disk_size_gb       = var.default_pool_os_disk_size_gb
    type                  = &quot;VirtualMachineScaleSets&quot;
    vnet_subnet_id        = var.vnet_subnet_id
    node_labels           = var.agents_labels
    tags                  = merge(local.tags, var.agents_tags)
  }

  network_profile {
    network_plugin     = var.network_plugin
    network_policy     = var.network_policy
    dns_service_ip     = var.net_profile_dns_service_ip
    docker_bridge_cidr = var.net_profile_docker_bridge_cidr
    service_cidr       = var.net_profile_service_cidr
  }

  role_based_access_control {
    enabled = true
    azure_active_directory {
      managed                = true
      admin_group_object_ids = var.rbac_aad_admin_group_object_ids
    }
  }

  identity {
    type = &quot;SystemAssigned&quot;
  }

  addon_profile {
    azure_policy {
      enabled = true
    }

    http_application_routing {
      enabled = false
    }

    oms_agent {
      enabled                    = true
      log_analytics_workspace_id = data.azurerm_log_analytics_workspace.log_analytics.id
    }
  }

  tags = local.tags

  lifecycle {
    ignore_changes = [
      default_node_pool
    ]
  }

}

resource &quot;azurerm_kubernetes_cluster_node_pool&quot; &quot;aksnp&quot; {
  lifecycle {
    ignore_changes = [
      node_count
    ]
  }
  for_each              = var.additional_node_pools
  kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id
  name                  = each.value.node_os == &quot;Windows&quot; ? substr(each.key, 0, 6) : substr(each.key, 0, 12)
  node_count            = each.value.node_count
  vm_size               = each.value.vm_size
  availability_zones    = each.value.zones
  max_pods              = each.value.max_pods
  os_disk_size_gb       = each.value.os_disk_size_gb
  os_type               = each.value.node_os
  vnet_subnet_id        = var.vnet_subnet_id
  node_taints           = each.value.taints
  enable_auto_scaling   = each.value.cluster_auto_scaling
  min_count             = each.value.cluster_auto_scaling_min_count
  max_count             = each.value.cluster_auto_scaling_max_count
}

resource &quot;kubernetes_namespace&quot; &quot;aks-namespace&quot; {
  metadata {
    name = var.namespace
  }
}
</code></pre>
<p><strong>data.tf</strong></p>
<pre><code>data &quot;azurerm_resource_group&quot; &quot;rg&quot; {
  name = var.resource_group_name
}
</code></pre>
<p><strong>lookups.tf</strong></p>
<pre><code>locals {

  environment_lookup = {
    dev  = &quot;d&quot;
    test = &quot;t&quot;
    int  = &quot;i&quot;
    prod = &quot;p&quot;
    prd  = &quot;p&quot;
    uat  = &quot;a&quot;
    poc  = &quot;d&quot;
    dr   = &quot;r&quot;
    lab  = &quot;l&quot;
  }

 
  lookup_result = lookup(local.environment_lookup, var.environment)

  tags = merge(
    data.azurerm_resource_group.rg.tags, {
      Directory      = &quot;tectcompany.com&quot;,
      PrivateDNSZone = var.private_dns_zone,
      Immutable      = &quot;False&quot;,
      ManagedOS      = &quot;True&quot;,
    }
  )
}

data &quot;azurerm_log_analytics_workspace&quot; &quot;log_analytics&quot; {
  name                = &quot;abc-az-lad2&quot;
  resource_group_name = &quot;abc-dev-aae&quot;
}
</code></pre>
<p><strong>variables.tf</strong></p>
<pre><code>variable &quot;secondary_region&quot; {
  description = &quot;Is this resource being deployed into the secondary (pair) region?&quot;

  default = false
  type    = bool
}

variable &quot;override_log_analytics_workspace&quot; {
  description = &quot;Override the vm log analytics workspace&quot;
  type        = string
  default     = null
}

variable &quot;override_log_analytics_resource_group_name&quot; {
  description = &quot;Overrides the log analytics resource group name&quot;
  type        = string
  default     = null
}

variable &quot;environment&quot; {
  description = &quot;The name of environment for the AKS Cluster&quot;
  type        = string
  default     = &quot;dev&quot;
}

variable &quot;identifier&quot; {
  description = &quot;The identifier for the AKS Cluster&quot;
  type        = number
  default     = &quot;001&quot;
}

variable &quot;kubernetes_version&quot; {
  description = &quot;Specify which Kubernetes release to use. The default used is the latest Kubernetes version available in the region&quot;
  type        = string
  default     = &quot;1.19.9&quot;
}

variable &quot;dns_prefix&quot; {
  description = &quot;The dns prefix for the AKS Cluster&quot;
  type        = string
  default     = &quot;odessa-sandpit&quot;
}

variable &quot;orchestrator_version&quot; {
  description = &quot;Specify which Kubernetes release to use for the orchestration layer. The default used is the latest Kubernetes version available in the region&quot;
  type        = string
  default     = null
}

variable &quot;agents_availability_zones&quot; {
  description = &quot;(Optional) A list of Availability Zones across which the Node Pool should be spread. Changing this forces a new resource to be created.&quot;
  type        = list(string)
  default     = null
}

variable &quot;agents_size&quot; {
  default     = &quot;Standard_D4s_v3&quot;
  description = &quot;The default virtual machine size for the Kubernetes agents&quot;
  type        = string
}

variable &quot;vnet_subnet_id&quot; {
  description = &quot;(Optional) The ID of a Subnet where the Kubernetes Node Pool should exist. Changing this forces a new resource to be created.&quot;
  type        = string
  default     = null
}

variable &quot;agents_labels&quot; {
  description = &quot;(Optional) A map of Kubernetes labels which should be applied to nodes in the Default Node Pool. Changing this forces a new resource to be created.&quot;
  type        = map(string)
  default     = {}
}

variable &quot;agents_tags&quot; {
  description = &quot;(Optional) A mapping of tags to assign to the Node Pool.&quot;
  type        = map(string)
  default     = {}
}

variable &quot;net_profile_dns_service_ip&quot; {
  description = &quot;(Optional) IP address within the Kubernetes service address range that will be used by cluster service discovery (kube-dns). Changing this forces a new resource to be created.&quot;
  type        = string
  default     = null
}

variable &quot;net_profile_docker_bridge_cidr&quot; {
  description = &quot;(Optional) IP address (in CIDR notation) used as the Docker bridge IP address on nodes. Changing this forces a new resource to be created.&quot;
  type        = string
  default     = null
}

variable &quot;net_profile_service_cidr&quot; {
  description = &quot;(Optional) The Network Range used by the Kubernetes service. Changing this forces a new resource to be created.&quot;
  type        = string
  default     = null
}

variable &quot;rbac_aad_admin_group_object_ids&quot; {
  description = &quot;Object ID of groups with admin access.&quot;
  type        = list(string)
  default     = null
}

variable &quot;network_policy&quot; {
  description = &quot;(Optional) The Network Policy to be used by the network profile of Azure Kubernetes Cluster.&quot;
  type        = string
  default     = &quot;azure&quot;
}

variable &quot;network_plugin&quot; {
  description = &quot;(Optional) The Network Plugin to be used by the network profile of Azure Kubernetes Cluster.&quot;
  type        = string
  default     = &quot;azure&quot;
}

variable &quot;enable_private_cluster&quot; {
  description = &quot;(Optional) Set this variable to true if you want Azure Kubernetes Cluster to be private.&quot;
  default     = true
}

variable &quot;default_pool_node_count&quot; {
  description = &quot;(Optional) The initial node count for the default pool of AKS Cluster&quot;
  type        = number
  default     = 3
}

variable &quot;default_pool_max_node_count&quot; {
  description = &quot;(Optional) The max node count for the default pool of AKS Cluster&quot;
  type        = number
  default     = 6
}

variable &quot;default_pool_min_node_count&quot; {
  description = &quot;(Optional) The min node count for the default pool of AKS Cluster&quot;
  type        = number
  default     = 3
}

variable &quot;default_pool_max_pod_count&quot; {
  description = &quot;(Optional) The max pod count for the default pool of AKS Cluster&quot;
  type        = number
  default     = 13
}

variable &quot;default_pool_os_disk_size_gb&quot; {
  description = &quot;(Optional) The size of os disk in gb for the nodes from default pool of AKS Cluster&quot;
  type        = string
  default     = &quot;64&quot;
}

variable &quot;additional_node_pools&quot; {
  type = map(object({
    node_count                     = number
    max_pods                       = number
    os_disk_size_gb                = number
    vm_size                        = string
    zones                          = list(string)
    node_os                        = string
    taints                         = list(string)
    cluster_auto_scaling           = bool
    cluster_auto_scaling_min_count = number
    cluster_auto_scaling_max_count = number
  }))
}
variable &quot;sku_tier&quot; {
  description = &quot;(Optional)The SKU Tier that should be used for this Kubernetes Cluster, possible values Free or Paid&quot;
  type        = string
  default     = &quot;Paid&quot;

  validation {
    condition     = contains([&quot;Free&quot;, &quot;Paid&quot;], var.sku_tier)
    error_message = &quot;SKU_TIER can only be either Paid or Free.&quot;
  }

}

variable &quot;la_sku&quot; {

  description = &quot;(Optional)The SKU Tier that should be used for Log Analytics. Multiple values are possible.&quot;
  type        = string
  default     = &quot;PerGB2018&quot;

  validation {
    condition     = contains([&quot;Free&quot;, &quot;PerNode&quot;, &quot;Premium&quot;, &quot;Standard&quot;, &quot;Standalone&quot;, &quot;Unlimited&quot;, &quot;CapacityReservation&quot;, &quot;PerGB2018&quot;], var.la_sku)
    error_message = &quot;SKU_TIER for Log Analytics can be can only be either of Free, PerNode, Premium, Standard, Standalone, Unlimited, CapacityReservation and PerGB2018(Default Value).&quot;
  }

}

variable &quot;resource_group_name&quot; {
  description = &quot;Resource Group for deploying AKS Cluster&quot;
  type = string
}

variable &quot;private_dns_zone&quot; {
  description = &quot;DNS prefix for AKS Cluster&quot;
  type = string
  default = &quot;testcluster&quot;
}

variable &quot;tla&quot; {
  description = &quot;Three Level acronym - three letter abbreviation for application&quot;
  type = string
  default = &quot;&quot;
  validation {
    condition     = length(var.tla) == 3
    error_message = &quot;The TLA should be precisely three characters.&quot;
  }
}

variable &quot;namespace&quot;{
 description = &quot;AKS Namespace&quot;
  type = string
}
</code></pre>
<p><strong>Finally</strong>, I am calling my module below to create the AKS cluster, LA, and Namespace for the AKS Cluster:</p>
<pre><code>provider &quot;azurerm&quot; {
   features {}
   #version = &quot;~&gt; 2.53.0&quot;
}
module &quot;aks-cluster1&quot; {

  source = &quot;../../&quot;
  resource_group_name = &quot;pst-aks-sandpit-dev-1&quot;
  tla = &quot;pqr&quot;
  additional_node_pools = { 
        pool1 = {
            node_count                      = &quot;1&quot;
            max_pods                       = &quot;110&quot;
            os_disk_size_gb                = &quot;30&quot;
            vm_size                        = &quot;Standard_D8s_v3&quot;
            zones                          = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;]
            node_os                        = &quot;Linux&quot;
            taints                         =  [&quot;kubernetes.io/os=windows:NoSchedule&quot;]
            cluster_auto_scaling           = true
            cluster_auto_scaling_min_count = &quot;2&quot;
            cluster_auto_scaling_max_count = &quot;4&quot;
        } 
                            }
  namespace = &quot;sample-ns&quot;
}
</code></pre>
<p><strong>Problem:</strong>
I get an error that no such host when terraform attempts to create the cluster.</p>
<p>I think that it is not able to connect to the cluster but I could be wrong. I do not know how it handles internally.</p>
<pre><code>Error: Post &quot;https://testdns-05885a32.145f13c0-25ce-43e4-ae46-8cbef448ecf3.privatelink.australiaeast.azmk8s.io:443/api/v1/namespaces&quot;: dial tcp: lookup testdns-05885a32.145f13c0-25ce-43e4-ae46-8cbef448ecf3.privatelink.australiaeast.azmk8s.io: no such host
</code></pre>

learner
<p>I'm one of the maintainers of the Terraform Kubernetes provider, and I see this particular issue pretty often. As a former devops person myself, I empathize with the struggle I keep seeing in this area. It's something I would really love to fix in the provider, if it were possible.</p>
<p>The issue you're facing is a limitation in Terraform core when <a href="https://www.terraform.io/docs/language/providers/configuration.html#provider-configuration-1" rel="noreferrer">passing an unknown value to a provider configuration block</a>. To quote their docs:</p>
<pre><code>You can use expressions in the values of these configuration arguments, 
but can only reference values that are known before the configuration is applied.
</code></pre>
<p>When you make a change to the underlying infrastructure, such the AKS cluster in this case, you're passing an unknown value into the Kubernetes provider configuration block, since the full scope of the cluster infrastructure is not known until after the change has been applied to the AKS cluster.</p>
<p>Although I did write the initial guide to show that <a href="https://github.com/hashicorp/terraform-provider-kubernetes/tree/614b5ed8ab2e962df855c0c8d4ca9c7ec50e88fe/_examples/aks#replacing-the-aks-cluster-and-re-creating-the-kubernetes--helm-resources" rel="noreferrer">it can be possible to work around some of these issues</a>, as you've found from experience, there are many edge cases that make it an unreliable and unintuitive process, to get the Kubernetes provider working alongside the underlying infrastructure. This is due to a long-standing  <a href="https://github.com/hashicorp/terraform/issues/4149" rel="noreferrer">limitation in Terraform</a>, that can't be fixed in any provider, but we do have plans to smooth out the bumps a little by adding <a href="https://github.com/hashicorp/terraform-provider-kubernetes/pull/1141" rel="noreferrer">better error messages upfront</a>, which would have saved you some headache in this case.</p>
<p>To solve this particular type of problem, the cluster infrastructure needs to be kept in a state separate from the Kubernetes and Helm provider resources. I have an example here which builds an AKS cluster in one apply and then manages the Kubernetes/Helm resources in a second apply. You can use this approach to build the most robust configuration for you particular use case:</p>
<p><a href="https://github.com/hashicorp/terraform-provider-kubernetes/tree/e058e225e621f06e393bcb6407e7737fd43817bd/_examples/aks" rel="noreferrer">https://github.com/hashicorp/terraform-provider-kubernetes/tree/e058e225e621f06e393bcb6407e7737fd43817bd/_examples/aks</a></p>
<p>I know this two-apply approach is inconvenient, which is why we continue to try and accommodate users in single-apply scenarios, and scenarios which contain the Kubernetes and cluster resources in the same Terraform state. However, until upstream Terraform can add support for this, the single-apply workflow will remain buggy and less reliable than separating cluster infrastructure from Kubernetes resources.</p>
<p>Most cases can be worked around using <code>depends_on</code> (to ensure the cluster is created before the Kubernetes resource), or by moving the cluster infrastructure into a separate module and running <code>terraform state rm module.kubernetes-config</code> or <code>terraform apply -target=module.aks-cluster</code>. But I think encouraging this kind of work-around will cause more headaches in the long run, as it puts the user in charge of figuring out when to use special one-off apply commands, rather than setting up Terraform to behave reliably and predictably from the start. Plus it can have unintended side-effects, like <a href="https://github.com/hashicorp/terraform/issues/27728#issuecomment-840093064" rel="noreferrer">orphaning cloud resources</a>.</p>
