<p>I have successfully deployed AKS Using Terragrunt through Atlantis, Now I want to set credentials to communicate with the Kubernetes API Server.</p>
<p>For that, I am setting up the <strong>KUBECONFIG</strong> Environment variable to authenticate with Kubernetes.</p>
<p>Below is the code that will run in Atlantis Container, so that we will have one-click deployment of pods or helm after setting credentials through Terraform code only.</p>
<pre><code>resource &quot;null_resource&quot; &quot;null&quot; {
 provisioner &quot;local-exec&quot; {
   command = &lt;&lt;-EOT
    echo &quot;$(terraform output kube_config)&quot; &gt; ~/.kube/azurek8s # Storing kube config credential file for kube api server authentication
    sed -i '1d;$d' ~/.kube/azurek8s # delete 1st and last line from output
  EOT
  }
  provisioner &quot;local-exec&quot; {
   command = &quot;export KUBECONFIG=~/.kube/azurek8s&quot; # setting up env variable for kubeconfig
  }
  provisioner &quot;local-exec&quot; {
   command = &quot;env&quot;
  }
}
</code></pre>
<p>After setting up the environment variable, I have added the env command to check whether actually environment variable is set or not.</p>

Ajinkya Bhabal
<p>Each <code>local-exec</code> will execute in its own shell environment, so there is no persistence between the second and third executions of your <code>local-exec</code>.</p>
<p>To set environment variables for your <code>local-exec</code>, you should use <a href="https://www.terraform.io/docs/provisioners/local-exec.html#environment" rel="noreferrer">environment</a>:</p>
<pre><code>  provisioner &quot;local-exec&quot; {
   command = &quot;env&quot;
   environment = {
       KUBECONFIG = &quot;~/.kube/azurek8s&quot;
   }
</code></pre>
