<p>I'm trying to use kubernetes-alpha provider in Terraform, but I have  &quot;Failed to construct REST client&quot; error message.
I'm using tfk8s to convert my yaml file to terraform code.</p>
<p>I make the seme declaration for the provider than kubernetes, and my kubernetes provider work correctely</p>
<pre><code>provider &quot;kubernetes-alpha&quot; {
  host                   = &quot;https://${data.google_container_cluster.primary.endpoint}&quot;
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(data.google_container_cluster.primary.master_auth[0].cluster_ca_certificate)
}

provider &quot;kubernetes&quot; {
  host                   = &quot;https://${data.google_container_cluster.primary.endpoint}&quot;
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(data.google_container_cluster.primary.master_auth[0].cluster_ca_certificate)
}
</code></pre>
<pre><code>resource &quot;kubernetes_manifest&quot; &quot;exemple&quot; {
  provider = kubernetes-alpha
  manifest = {
     # result of tfk8s
  }
}

</code></pre>
<p><a href="https://i.stack.imgur.com/rJoT9.png" rel="nofollow noreferrer">the error message</a></p>
<p>somebody can help ?</p>

握草行天下
<p>It's rather unfortunate <code>kubernetes_manifest</code> doesn't allow it and that's due to it trying to resolve types. For the type resolution the devs chose to contact the Kubernetes API server. When you have a single <code>main.tf</code> (or single <code>plan</code>/<code>apply</code>) with creating the Kubernetes cluster <strong>and</strong> and the same time <strong>using</strong> the cluster you get into a missing dependency once you reach <code>kubernetes_manifest</code>. That's because of the Terraform files not being read just-in-time but only single-pass and execute anything a provider defines.</p>
<p>If you have a provider's configuration depending on the previous provider's resources being created, you get into these problems. For <code>kubernetes_manifest</code> however there's a quick solution - just use <a href="https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/kubectl_manifest" rel="nofollow noreferrer"><code>kubectl_manifest</code></a> from the <a href="https://github.com/gavinbunney/terraform-provider-kubectl" rel="nofollow noreferrer"><code>gavinbunney/kubectl</code> provider</a> and either put the yaml to a heredoc or into separate file/folder.</p>
<p>Raw:</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;some-name&quot; {
  yaml_body = &lt;&lt;YAML
some: yaml
with: ${var.something}
YAML
}
</code></pre>
<p>File:</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;some-name&quot; {
  yaml_body = &quot;${file(&quot;./file.yaml&quot;)}&quot;
}
</code></pre>
<p>Multiple files in folder:</p>
<pre><code>data &quot;kubectl_path_documents&quot; &quot;other-name&quot; {
  pattern = &quot;./folder/*.yaml&quot;
}

resource &quot;kubectl_manifest&quot; &quot;some-name&quot; {
  for_each  = toset(data.kubectl_path_documents.other-name.documents)
  yaml_body = each.value
}
</code></pre>
<p>There is also built-in functionality for templating in the <code>kubectl</code> provider:</p>
<blockquote>
<p>A set of helpful data resources to process directories of yaml files and inline templating is available. (<a href="https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs" rel="nofollow noreferrer">source</a>)</p>
</blockquote>
<p>so you can use <code>${var.something}</code> in the file itself.</p>
<p>If that doesn't help, there are always these for the rescue as the <code>yaml_body</code> is just a string thus you can work with it as with any other string:</p>
<ul>
<li><a href="https://www.terraform.io/language/functions/file" rel="nofollow noreferrer"><code>file</code></a></li>
<li><a href="https://www.terraform.io/language/functions/templatefile" rel="nofollow noreferrer"><code>templatefile</code></a></li>
<li><a href="https://www.terraform.io/language/functions/replace" rel="nofollow noreferrer"><code>replace()</code></a></li>
</ul>
<p>Note: Intermixing <code>kubernetes</code> provider and <code>kubectl</code> provider doesn't seem to cause issues and I've been using it for a long time. The issue seems to be happening only for <code>kubernetes_manifest</code> and only because Terraform wants to resolve the types (which is understandable but also a flawed chicken&amp;egg problem), so any other resource such as namespaces, pods, the standard stuff works.</p>
