<p>I have two emails associated with two separate gcloud projects.</p>
<p>I can easily switch the projects with:</p>
<pre><code>$ gcloud auth list
  Credentialed Accounts
ACTIVE  ACCOUNT
        first@project1
*       second@project2

$ gcloud config set account first@project1
</code></pre>
<p>I can then see, that gcloud did change the active account. I can also do this with:</p>
<pre><code>$ gcloud config configurations list
...
$ gcloud config configurations set project1
</code></pre>
<p>And I can see the active configuration changes.</p>
<p>However it does not seem to have any effect for <code>kubectl</code> and <code>terraform</code> commands as they still use the previous configuration.</p>
<p>What am i doing wrong? How should I switch between the projects? It seems it has something to do with <code>application-default</code> account, but that looks it cannot be easily switched without relogin?</p>
<p><strong>Edit</strong>: to precise the question:</p>
<p>What would be a correct sequence of commands to switch between gcloud auths (eg. first@project1, second@project2) so that it is usable in Kubernetes, Terraform and others?</p>

Vojtěch
<p>Kubectl and terraform have own config or we can say context</p>
<p>for kubectl you need to change the cluster config using</p>
<pre><code>kubectl config get-contexts

kubectl config use-context &lt;cluster-name&gt;
</code></pre>
<p>Or else each time you have set the context of Kubernetes cluster using Gcloud and it will get auto changed for kubectl</p>
<p><code>gcloud container clusters get-credentials cluster-name</code> which takes the --project also.</p>
<p>Read more at : <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl" rel="nofollow noreferrer">https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl</a></p>
<p>For changing project in terraform there are different ways</p>
<ul>
<li>Using different serviceaccount keys JSON</li>
<li>Changing project config inside terraform provider</li>
<li>Setting up environment variable <code>GOOGLE_APPLICATION_CREDENTIALS</code></li>
</ul>
<p>setting project inside the Provider</p>
<pre><code>provider &quot;google&quot; {
  project     = &quot;my-project-id&quot;
  region      = &quot;us-central1&quot;
  zone        = &quot;us-central1-c&quot;
}
</code></pre>
<p><a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference</a></p>
<p>Best approach to use : <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#credentials-1" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#credentials-1</a></p>
<p>As you are writing IAC so all config in code.</p>
<p>List of all possible methods for <strong>authentication</strong> terraform:</p>
<p><a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication</a></p>
