3

I have successfully deployed AKS Using Terragrunt through Atlantis, Now I want to set credentials to communicate with the Kubernetes API Server.

For that, I am setting up the KUBECONFIG Environment variable to authenticate with Kubernetes.

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.

resource "null_resource" "null" {
 provisioner "local-exec" {
   command = <<-EOT
    echo "$(terraform output kube_config)" > ~/.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 "local-exec" {
   command = "export KUBECONFIG=~/.kube/azurek8s" # setting up env variable for kubeconfig
  }
  provisioner "local-exec" {
   command = "env"
  }
}

After setting up the environment variable, I have added the env command to check whether actually environment variable is set or not.

1 Answers1

11

Each local-exec will execute in its own shell environment, so there is no persistence between the second and third executions of your local-exec.

To set environment variables for your local-exec, you should use environment:

  provisioner "local-exec" {
   command = "env"
   environment = {
       KUBECONFIG = "~/.kube/azurek8s"
   }
Marcin
  • 215,873
  • 14
  • 235
  • 294