I have multiple GCP accounts for multiple clients and I handle their infrastructure using Terraform.
To change between different projects/GCP accounts, I use the gcloud config configurations activate <configuration name> command, which works great for using the CLI manually.
However, when using Terraform, you must use an application-default account, saved to ~/.config/gcloud/application_default_credentials.json and NOT attached to a configuration.
So when I switch configurations, I also have to log in to the associated application-default account manually, which is quite cumbersome.
My current solution is to log in first, then manually save the .json credentials to an explicit path ~/.config/gcloud/<configuration name>/application_default_credentials.json, I have the GOOGLE_APPLICATION_CREDENTIALS set to this path in my Terraform commands in a Makefile.
Not ideal, but it allows me to just switch configurations using the CLI and not have to use the web login flow again to set the ADC.
Is there a better way to do this? Can I attach the ADC creds to a configuration and have gcloud manage it for me?
gcloud auth application-default login. See Application Default Credentials.gcloud auth activate-service-account(the key can then be deleted), the Service Account appears ingcloud auth listand will be setgcloud config get accountand should be part of thegcloud configuration. I'm not going to recommend this solution though and provide it for completeness. You would need to determine whether a compromised Service Account is less|more powerful than a compromised user account (which is what happens if someone stealsapplication-default-credentials.json).gcloud configurationsbecause they change your host's globalgcloudstate (you switch to an existing shell thinking you're using one set of credentials and realize you're not when you accidentally delete the world). Switching configurations and user account ADCs isn't onerous. You could consider extendinggcloud(it's Python) to encompass both steps in one. I think you'd be better scripting a solution that is more explicit in its behavior, changes only the current (!) context and changes the terminal background to uniquely identify the tenant.