<p>I've created a GKE cluster with Terraform and I also want to manage Kubernetes with Terraform as well. However, I don't know how to pass GKE's credentials to the <code>kubernetes</code> provider.</p>

<p>I followed the <a href="https://www.terraform.io/docs/providers/google/d/datasource_client_config.html" rel="noreferrer">example in the <code>google_client_config</code> data source documentation</a> and I got </p>

<blockquote>
  <p><strong>data.google_container_cluster.cluster.endpoint</strong> is null</p>
</blockquote>

<p>Here is my failed attempt <a href="https://github.com/varshard/gke-cluster-terraform/tree/title-terraform" rel="noreferrer">https://github.com/varshard/gke-cluster-terraform/tree/title-terraform</a></p>

<p><code>cluster.tf</code> is responsible for creating a GKE cluster, which work fine.</p>

<p><code>kubernetes.tf</code> is responsible for managing Kubernetes, which failed to get GKE credential.</p>

<p>You don't need the <a href="https://www.terraform.io/docs/providers/google/d/google_container_cluster.html" rel="nofollow noreferrer"><code>google_container_cluster</code> data source</a> here at all because the <a href="https://www.terraform.io/docs/providers/google/r/container_cluster.html#endpoint" rel="nofollow noreferrer">relevant information</a> is also in the <a href="https://www.terraform.io/docs/providers/google/r/container_cluster.html" rel="nofollow noreferrer"><code>google_container_cluster</code> resource</a> that you are creating in the same context.</p>
<p>Data sources are for accessing data about a resource that is created either entirely outside of Terraform or in a different Terraform context (eg different state file and different directory that is <code>terraform apply</code>'d).</p>
<p>I'm not sure how you're in your current state where the data source is selecting an existing container cluster and then you define a resource to create that container cluster using the outputs of the data source but this is way overcomplicated and slightly broken - if you destroyed everything and reapplied it wouldn't work as is.</p>
<p>Instead you should remove the <code>google_container_cluster</code> data source and amend your <code>google_container_cluster</code> resource to instead be:</p>
<pre><code>resource &quot;google_container_cluster&quot; &quot;cluster&quot; {
  name     = &quot;${var.project}-cluster&quot;
  location = var.region

  # ...
}
</code></pre>
<p>And then refer to this resource in your <code>kubernetes</code> provider:</p>
<pre><code>provider &quot;kubernetes&quot; {
  load_config_file = false
  host                   = &quot;https://${google_container_cluster.cluster.endpoint}&quot;
  cluster_ca_certificate = base64decode(google_container_cluster.cluster.master_auth.0.cluster_ca_certificate)
  token                  = data.google_client_config.current.access_token
}
</code></pre>
