<p>I am learning how to translate <code>kubectl</code> deployments to terraform. I am currently facing issues getting services to work as intended with terraform provider <code>kubectl</code> once I specify a namespace.</p>
<p>I have confirmed the terraform script works when doing the equivalent <code>kubectl apply</code> to the default <code>namespace</code>.</p>
<p>What is the proper methodology in terraform using the <code>kubectl</code> provider to <code>apply -n namespace</code>?</p>
<p>The two different approaches I have tried are:</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;example&quot; {
        override_namespace = kubernetes_namespace_v1.namespace.metadata[0].name
        yaml_body = file(&quot;${path.cwd},deploy.yaml&quot;)
    }
</code></pre>
<p>And also:</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;example&quot; {
        yaml_body = file(&quot;${path.cwd},deploy.yaml&quot;)
    }
</code></pre>
<p>with adding the namespace to the <code>deploy.yaml</code>:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
  namespace: namespace
...
---
apiVersion: v1
kind: Service
metadata:
  name: example
  namespace: namespace
</code></pre>
<p>Then when I try to confirm that the service is functioning as intended via:</p>
<pre><code>kubectl logs example-6878fd468-9vgkm
</code></pre>
<blockquote>
<p>Error from server (NotFound): pods &quot;example-6878fd468-9vgkm&quot; not found</p>
</blockquote>

Matt - Block-Farms.io
<p>Your terraform description is fine. You need to specify the <code>-n</code> switch to your <code>kubectl logs</code> command as well, to detect the pod in the right namespace.</p>
<pre><code>kubectl logs example-6878fd468-9vgkm -nexample
</code></pre>
