Is it possibly to overwrite the Kubeconfig with terraform's Kubernetes provider

4/30/2020

I wanted to run terraform and then be able to run kubectl in the cli right after terraform completes. Or is this something you don't do. I would want to make a script to run kubectl commands after terraform finishes creating the cluster.

I have this and I am assuming I could write terraform kubernetes code but I don't believe it is overwriting the cli's kubeconfig referenced file.

provider "kubernetes" {
  load_config_file        = false
  host                    = azurerm_kubernetes_cluster.cluster_1.kube_config.0.host
  username                = azurerm_kubernetes_cluster.cluster_1.kube_config.0.username
  password                = azurerm_kubernetes_cluster.cluster_1.kube_config.0.password
  client_certificate      = base64decode(azurerm_kubernetes_cluster.cluster_1.kube_config.0.client_certificate)
  client_key              = base64decode(azurerm_kubernetes_cluster.cluster_1.kube_config.0.client_key)
  cluster_ca_certificate  = base64decode(azurerm_kubernetes_cluster.cluster_1.kube_config.0.cluster_ca_certificate)
}
-- mjwrazor
azure-cli
terraform-provider-kubernetes

1 Answer

5/5/2020

If I understand correctly, you want to add a context inside your kube config file after creating a cluster. Maybe running az aks get-credentials using Terraform after creation will work?

resource "null_resource" "add_context" {
    provisioner "local-exec" {
        command = "az aks get-credentials --resource-group ${azurerm_kubernetes_cluster.cluster_1.resource_group_name} --name ${azurerm_kubernetes_cluster.cluster_1.name} --overwrite-existing"
    } 

    depends_on = [azurerm_kubernetes_cluster.cluster_1]
}
-- Daniel
Source: StackOverflow