Home > Net >  Terraform Azure AKS Cluster not exporting kubeconfig file
Terraform Azure AKS Cluster not exporting kubeconfig file

Time:10-23

I am trying to provision a kubernetes cluster on Azure (AKS) with Terraform. The provisioning works quite well but I can't get the kubeconfig from kube_config_raw exported to a file.

Below is my main.tf and outputs.tf. I supressed the resource_group and user_assigned_identity resources.

This is a resource I used for creating the configuration: enter image description here


Bonus: is it possible to export it directly to the existing ~/.kube/config file? Like the az aks get-credentials command does?

Path where the az aks get-credentials --resource-group myresourcegroup --name myCluster stores the config file:

enter image description here

Code to save the script in the same path as az command:

resource "local_file" "kubeconfig" {
  depends_on   = [azurerm_kubernetes_cluster.aks]
  filename     = "C:/Users/user/.kube/config" this is where the config file gets stored
  content      = azurerm_kubernetes_cluster.aks.kube_config_raw
}

Output:

Exisitng config file in /.kube/config

enter image description here

New File overwrites the existing file :

enter image description here

Note: Using local_file block here will completely overwrite the file not appending the context to the previous one . If you are looking for merging the content in a single file like az command does , then its not possible from terraform.

  • Related