<p>We are running a terraform to create a GKE Cluster and using the below to create a local kubeconfig file after the creation of the cluster.</p>
<pre><code>module &quot;gke_auth&quot; {
  source = &quot;terraform-google-modules/kubernetes-engine/google//modules/auth&quot;
  depends_on   = [module.gke]
  project_id   = var.project_id
  location     = module.gke.location
  cluster_name = module.gke.name
}

resource &quot;local_file&quot; &quot;kubeconfig&quot; {
  content  = module.gke_auth.kubeconfig_raw
  filename = &quot;kubeconfig&quot;
}
</code></pre>
<p>Post that we would like to continue and deploy istio and other deployments on the cluster and to connect to the cluster we are referring kubeconfig file as below.</p>
<pre><code>provider &quot;helm&quot; {
  kubernetes {
    config_path = &quot;kubeconfig&quot;
  }
}

provider &quot;kubernetes&quot; {
  config_path = &quot;kubeconfig&quot;
}
</code></pre>
<p>But as soon as we run apply command below warning is shown.</p>
<pre><code>Invalid attribute in provider configuration

  with provider[&quot;registry.terraform.io/hashicorp/kubernetes&quot;],
  on main.tf line 42, in provider &quot;kubernetes&quot;:
  42: provider &quot;kubernetes&quot; {

'config_path' refers to an invalid path: &quot;kubeconfig&quot;: stat kubeconfig: no such file or directory
</code></pre>
<p>It is because initially the file is not there but it will be created eventually once the cluster is created. But the problem is after applying the template the session state is not refreshed automatically and even though the kubeconfig file has been created, it throws the below error and exits the execution.</p>
<pre><code>Error: Post &quot;http://localhost/api/v1/namespaces&quot;: dial tcp 127.0.0.1:80: connect: connection refused

Invalid attribute in provider configuration

  with provider[&quot;registry.terraform.io/hashicorp/kubernetes&quot;],
  on main.tf line 42, in provider &quot;kubernetes&quot;:
  42: provider &quot;kubernetes&quot; {

'config_path' refers to an invalid path: &quot;.kubeconfig&quot;: stat .kubeconfig: no such file or directory
</code></pre>
<p>Please suggest how to make this work?</p>

<p>We have this issue with the setup below.</p>
<p>For the first module, we added an output block.</p>
<pre><code>module &quot;gke_auth&quot; {
  source = &quot;terraform-google-modules/kubernetes-engine/google//modules/auth&quot;
  depends_on   = [module.gke]
  project_id   = var.project_id
  location     = module.gke.location
  cluster_name = module.gke.name
}

resource &quot;local_file&quot; &quot;kubeconfig&quot; {
  content  = module.gke_auth.kubeconfig_raw
  filename = &quot;kubeconfig&quot;
}

output &quot;kubeconfig_file&quot; {
      value = &quot;${path.cwd}/kubeconfig&quot;
    }
</code></pre>
<p>For the second module we made below changes:</p>
<pre><code>data &quot;terraform_remote_state&quot; &quot;kubeconfig_file&quot; {
  backend = &quot;local&quot;

  config = {
    path = &quot;${path.module}/../dirA/terraform.tfstate&quot;
  }
}

provider &quot;helm&quot; {
  kubernetes {
    config_path = &quot;${data.terraform_remote_state.kubeconfig_file.outputs.kubeconfig_file}&quot;
  }
}

provider &quot;kubernetes&quot; {
  config_path = &quot;${data.terraform_remote_state.kubeconfig_file.outputs.kubeconfig_file}&quot;
}
</code></pre>
<p><em>Note: In a similar way we can access variables from the different module or stack in a different directory</em></p>
