hotmeatballsoup
hotmeatballsoup

Reputation: 615

Adding multiple GCP accounts to gcloud, configuring them and switching between them, by example

I have two different GCP projects managed by two different organizations that are both tied to my email. Meaning when I log in to GCP Console I see them under my projects drop-downs.

I somehow (while onboarding with the first organization) configured my gcloud installation to successfully connect to the first org/project:

myuser ~ % gcloud config list
[compute]
region = us-east4
zone = us-east1-b
[core]
account = [email protected]
disable_usage_reporting = True
project = project-1

Your active configuration is: [default]


Updates are available for some Cloud SDK components.  To install them,
please run:
  $ gcloud components update



To take a quick anonymous survey, run:
  $ gcloud survey

I am now trying to add a configuration/project to my gcloud config so that I can switch back and forth between both projects throughout the day as needed:

myuser ~ % gcloud config configurations create project-2
Created [project-2].
Activated [project-2].

But now, when I go to list my available configurations I only see project-2:

myuser ~ % gcloud config list                               
[core]
disable_usage_reporting = True

Your active configuration is: [project-2]

What command can I run to now view both project configurations? And how do I connect project-2 to my email ([email protected]) and credentials?

Upvotes: 0

Views: 1048

Answers (1)

DazWilkin
DazWilkin

Reputation: 40091

I recommend using a name other than project-2 for the configuration as that could become confusing.

NOTE My original answer was incorrect. Switching between configurations switches between active accounts but the list of authenticated accounts is preserved across configurations.

A configuration encapsulates a gcloud "environment". So, after creating a configuration, you'll need to e.g. gcloud config set account to set any existing (authenticated) accounts as active or gcloud auth login to add new accounts to the list.

NOTE Once you've run gcloud auth login under any configuration, the list of accounts is preserved across all configurations but the active account is retained per configuration.

One challenge with setting configs is that gcloud commands become implicit, e.g. gcloud compute instances delete my-instance using configs could delete from the production project using the admin account if you're not careful to juggle gcloud config sets.

An alternative to configs (and my preference) is to always explicitly specify the configuration. Yes, it's more tedious and yes, you can still become complacent doing this, but I find it's more convenient:

i.e. always etc.

gcloud do-something \
--project=${PROJECT} \
--account=${ACCOUNT}

I strongly encourage you to set flags rather than use config when you're scripting gcloud commands. In this case, the additional typing is an up-front cost with a longer-term benefit of precision.

Another reason not to use configs. A configuration is globally applicable. If you use gcloud in multiple shells, changing the config in one shell, because it's global, will change the settings across shells (on the same machine).

Upvotes: 1

Related Questions