I have two Google Cloud service accounts; one for each of my two projects.
# ACCOUNTS [email protected] [email protected]
I can tell gcloud
which account I need to use before executing a command:
gcloud set account [ACCOUNT]
Question: Is there any way I can configure gcloud
and gsutil
so that they'll be used for operations performed in their respective project without me having to switch between these accounts manually all the time?
I'm managing instances in one project and I upload/download files from buckets in another project. It becomes quite tedious to have to perform gcloud set_account [ACCOUNT]
all the time in-between commands.
I need to be running long-running commands in both projects at the same time which causes me to think I will fall into a pit if I activate/de-activate the accounts used for these commands.
Perhaps my only option is to run google-cloud-sdk from two different Docker containers?
If you want to switch the account used by the gcloud CLI on a per-invocation basis, override the active account using the --account flag.
If you want to maintain sub-organizations or departments within your company as isolated entities with no central administration, you can set up multiple Google Workspace or Cloud Identity accounts. Each account will come with a single organization resource associated with a primary domain.
You have several options here:
The Cloud SDK respects environment variables specifying properties. gcloud config set account
is shorthand for gcloud config set core/account
, so the corresponding property is CLOUDSDK_CORE_ACCOUNT
.
You can do something like:
$ [email protected] gcloud ... $ [email protected] gcloud ...
Which should get you the result you're interested in.
If you need more than one property changed, the Cloud SDK offers a named configuration abstraction. See the docs for full details, but you can run:
$ gcloud config configurations create my-project1-config $ gcloud config configurations activate my-project1-config $ gcloud auth login # or activate-service-account $ gcloud config set project project1 # and any other configuration you need to do $ $ gcloud config configurations create my-project2-config $ gcloud config configurations activate my-project2-config $ gcloud auth login # or activate-service-account $ gcloud config set project project2 # and any other configuration you need to do $ $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project1-config gcloud ... $ CLOUDSDK_ACTIVE_CONFIG_NAME=my-project2-config gcloud ...
In the most extreme case, you can maintain separate Cloud SDK configuration directories. The default (on *nix) is ~/.config/gcloud
:
$ CLOUDSDK_CONFIG=/tmp/tmpconfig1 gcloud auth login $ CLOUDSDK_CONFIG=/tmp/tmpconfig2 gcloud auth login
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With