<p>I am learning Kubernetes, and my objective is to deploy a Kubernetes but using Yaml files through terraform (<a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/service#argument-reference" rel="nofollow noreferrer">Kubernetes provider</a>). Let me illustrate with an example:</p>
<p>As <a href="https://stackoverflow.com/questions/53667413/is-it-possible-from-file-configmap-using-kubernetes-config-map-resource">per</a> I understand that I can have a ConfigMap as a resource, like:</p>
<pre><code>resource &quot;kubernetes_config_map&quot; &quot;config&quot; {
  metadata {
    namespace = &quot;metallb_system&quot;
    name = &quot;config&quot;
  }
  data {
    config = &quot;${file(${path.module}/config.yml)}&quot;
  }
}
</code></pre>
<p>And now I would like to deploy a service, which in HCL looks like:</p>
<pre><code>resource &quot;kubernetes_service&quot; &quot;nginx&quot; {

  metadata {
    name = &quot;nginx-example-bla&quot;
  }
  spec {
    selector = {
      App = kubernetes_pod.airflow.metadata[0].labels.App
    }
    port {
      port        = 80
      target_port = 8080
    }

    type = &quot;LoadBalancer&quot;
  }
}
</code></pre>
<p>But I would like to do it with yaml instead of HCL.</p>
<p>So (hypothetically), I want to be able do something like:</p>
<pre><code>resource &quot;kubernetes_service&quot; &quot;nginx&quot; {
  file = kubernetes_config_map.config  # Not sure if ConfigMap is the right usage here but I want to be able to deploy a service through Yaml
}
</code></pre>

<p>Terraform only tracks resources added with HCL, using <code>file()</code> only adds contents. So if you want terraform to manage your k8s resources (detect changes or the deletion of those resources), you'll have to stick with that.</p>
<p>If you simply want to deploy your YAML files using terraform instead, you can run commands using a &quot;null_resource&quot;:</p>
<pre><code>resource &quot;null_resource&quot; &quot;deploy-yaml&quot; {

  provisioner &quot;local-exec&quot; {
      command = &quot;kubectl apply -f xxx.yaml&quot;
  }
}
</code></pre>
<p>If you do this, keep in mind, that terraform does not have any change detection now. So if the YAML changed, terraform does not know about it. You'd have to trigger it by quick and dirty changing the resource name or using some always changing value (for example a random_uuid) to rerun the script.</p>
