<p>I am using the <a href="https://www.terraform.io/docs/providers/kubernetes/index.html" rel="nofollow noreferrer">Kubernetes Provider</a> to describe services/pods in Terraform.</p>

<p>It can get confusing using the Hashicorp Configuration Language to define <a href="https://www.terraform.io/docs/providers/kubernetes/r/pod.html" rel="nofollow noreferrer">kubernetes_pod</a> or <a href="https://www.terraform.io/docs/providers/kubernetes/r/service.html" rel="nofollow noreferrer">kubernetes_service</a> resources because the Kubernetes documentation describes everything in YAML which it means you need to translate it into HCL.</p>

<p>Is it possible to define pods as YAML and use them with <code>kubernetes_pod</code> and <code>kubernetes_service</code> resources as templates?</p>

<p>The <code>hashicorp/kubernetes</code> provider does not support raw YAML/JSON, and they have no intention of implementing it.</p>
<p>The possible solutions are:</p>
<ul>
<li><a href="https://awesomeopensource.com/project/sl1pm4t/k2tf" rel="nofollow noreferrer">K2tf</a>, a tool for converting Kubernetes RAW YAML manifests into Terraform HCL for the <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest" rel="nofollow noreferrer">Kubernetes provider</a>.</li>
<li>Use an alternative community Kubernetes provider, such   as <a href="https://registry.terraform.io/providers/gavinbunney/kubectl/latest" rel="nofollow noreferrer">gavinbunny/kubectl</a>, which does support raw YAML and can track each resource and the attributes in Terraform state, unlike the kubernetes-alpha provider.</li>
<li>Another solution is to use the <a href="https://registry.terraform.io/providers/hashicorp/kubernetes-alpha" rel="nofollow noreferrer">hashicorp/kubernetes-alpha</a> provider, you can pass in either a Terraform object or convert raw YAML manifest into a TF object for using in the provider resource. The downside is that the attributes are not tracked as individual objects and thus a change will cause the entire resource to be tainted.</li>
</ul>
<h3>Using the <a href="https://registry.terraform.io/providers/gavinbunney/kubectl/latest" rel="nofollow noreferrer">kubectl</a> provider.</h3>
<p>This core of this provider is the <code>kubectl_manifest</code> resource, allowing free-form yaml to be processed and applied against Kubernetes. This yaml object is then tracked and handles creation, updates and deleted seamlessly - including drift detection. This provider is ideal if you want to track the manifest in Terraform:</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;test&quot; {
    yaml_body = file(&quot;path/to/manifest.yaml&quot;)
}
</code></pre>
<h3>Using the <a href="https://registry.terraform.io/providers/hashicorp/kubernetes-alpha" rel="nofollow noreferrer">kubernetes-alpha</a> provider</h3>
<p>The <code>kubernetes_manifest</code> represents one Kubernetes resource as described in the manifest attribute. The manifest value is the HCL transcription of a regular Kubernetes YAML manifest. To transcribe an existing manifest from YAML to HCL, use the Terrafrom built-in function <code>yamldecode()</code>, or use the <a href="https://github.com/jrhouston/tfk8s" rel="nofollow noreferrer">tfk8s</a> tool to convert YAML into manifest attributes for the kubernetes-alpha provider manifest resource.</p>
<p>Example using <code>yamldecode</code>:</p>
<pre><code>resource &quot;kubernetes_manifest&quot; &quot;service&quot; {
  provider = kubernetes-alpha

  manifest = yamldecode(file(&quot;path/to/manifest.yaml&quot;))
}
</code></pre>
<hr />
<p><strong>Why doesn't the kubernetes provider support RAW YAML?</strong></p>
<blockquote>
<p>Supporting YAML/JSON in <code>hashicorp/kubernetes</code> <a href="https://github.com/hashicorp/terraform/pull/3132" rel="nofollow noreferrer">was considered before</a> (the very first proposal of K8S provider was exactly that) and during the initial implementation of this provider and we decided not to do it.</p>
</blockquote>
<p>The reason is that you can't accurately track resources created from RAW YAML as Terraform objects.</p>
<blockquote>
<p>From Terraform's developer perspective it is very tricky to get around
the way K8S API works where you send an array [a, b, c] to the Create
API and then you Get back [a, b, c, d]. This happens for example with
pods that get some secret volumes attached automatically, but happens
with most other resources I had the chance to play with. The
whitelisting/blacklisting is tricky part.</p>
</blockquote>
