<p>When I use Terraform to create a cluster in GKE everything works fine and as expected.</p>

<p>After the cluster is created, I want to then use Terraform to deploy a workload. </p>

<p>My issue is, how to be able to point at the correct cluster, but I'm not sure I understand the best way of achieving this.  </p>

<p>I want to automate the retrieval of the clusters kubeconfig file- the file which is generally stored at <code>~/.kube/config</code>. This file is updated when users run this <a href="https://cloud.google.com/sdk/gcloud/reference/container/clusters/get-credentials" rel="nofollow noreferrer">command</a> manually to authenticate to the correct cluster.</p>

<p>I am aware if this file is stored on the host machine (the one I have Terraform running on) that it's possible to point at this file to authenticate to the cluster like so:</p>

<pre><code>provider kubernetes {
  # leave blank to pickup config from kubectl config of local system
  config_path = "~/.kube/config"
}
</code></pre>

<p>However, running <a href="https://cloud.google.com/sdk/gcloud/reference/container/clusters/get-credentials" rel="nofollow noreferrer">this</a>  command to generate the kubeconfig requires Cloud SDK to be installed on the same machine that Terraform is running on, and its manual execution doesn't exactly seem very elegant. </p>

<p>I am sure I must be missing something in how to achieve this. </p>

<p>Is there a better way to retrieve the kubeconfig file via Terraform from a cluster created by Terraform?</p>

nmh
<p>Actually, there is another way to access to fresh created gke.</p>

<pre><code>data "google_client_config" "client" {}

provider "kubernetes" {
  load_config_file = false
  host = google_container_cluster.main.endpoint
  cluster_ca_certificate = base64decode(google_container_cluster.main.master_auth.0.cluster_ca_certificate)
  token = data.google_client_config.client.access_token
}
</code></pre>
