<p>I am trying to create/apply this kubectl .yaml file <a href="https://github.com/Azure/aad-pod-identity/blob/master/deploy/infra/deployment.yaml" rel="nofollow noreferrer">https://github.com/Azure/aad-pod-identity/blob/master/deploy/infra/deployment.yaml</a>  via Terrraform`s null_resource to the AKS to install Azure AD Pod Identity. It needed to deploy Azure Gateway Ingress Controller.
Using Windows 10 with VS Code</p>
<p><strong>main.tf:</strong></p>
<pre><code>data &quot;template_file&quot; &quot;aad_pod&quot; {
  template = &quot;${file(&quot;${path.module}/templates/aad_pod.yaml&quot;)}&quot;
}

resource &quot;null_resource&quot; &quot;aad_pod_deploy&quot; {
  triggers = {
    manifest_sha1 = &quot;${sha1(&quot;${data.template_file.aad_pod.rendered}&quot;)}&quot;
  }

  provisioner &quot;local-exec&quot; {
    command = &quot;kubectl apply -f -&lt;&lt;EOF\n${data.template_file.aad_pod.rendered}\nEOF&quot;
  }
}
</code></pre>
<p>After terraform apply I have this error:</p>
<pre><code>Error: Error running command 'kubectl apply -f -&lt;&lt;EOF
'cutted listing of yaml file'
EOF': exit status 1. Output: &lt;&lt; was unexpected at this time.
</code></pre>
<p>Any help will be appreciated</p>

S0meth1ng
<p>Because of differences between Unix-like operating systems and Windows, it's rarely possible to use <code>local-exec</code> in a portable way unless your use-case is very simple. This is one of the reasons why <a href="https://www.terraform.io/docs/provisioners/index.html#provisioners-are-a-last-resort" rel="noreferrer">provisioners are a last resort</a>.</p>
<p>I think the most portable answer would be to use <a href="https://registry.terraform.io/providers/hashicorp/kubernetes" rel="noreferrer">the official Kubernetes provider</a> to interact with Kubernetes here. Alternatively, if using <code>kubectl</code>'s input format in particular is important for what you are doing, you could use <a href="https://github.com/gavinbunney/terraform-provider-kubectl" rel="noreferrer">a community-maintained <code>kubectl</code> provider</a>.</p>
<pre><code>resource &quot;kubectl_manifest&quot; &quot;example&quot; {
  yaml_body = data.template_file.aad_pod.rendered
}
</code></pre>
<hr />
<p>If you have a strong reason to use the <code>local-exec</code> provisioner rather than a native Terraform provider, you'll need to find a way to write a command that can be interpreted in a compatible way by both a Unix-style shell and by Windows's command line conventions. I expect it would be easier to achieve that by writing the file out to disk first and passing the filename to <code>kubectl</code>, because that avoids the need to use any special features of the shell and lets everything be handled by <code>kubectl</code> itself:</p>
<pre><code>resource &quot;local_file&quot; &quot;aad_pod_deploy&quot; {
  filename = &quot;${path.module}/aad_pod.yaml&quot;
  content  = data.template_file.aad_pod.rendered

  provisioner &quot;local-exec&quot; {
    command = &quot;kubectl apply -f ${self.filename}&quot;
  }
}
</code></pre>
<p>There are still some caveats to watch out for with this approach. For example, if you run Terraform under a directory path containing spaces then <code>self.filename</code> will contain spaces and therefore probably won't be parsed as you want by the Unix shell or by the <code>kubectl</code> Windows executable.</p>
