Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple service accounts with gcloud?

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?

like image 397
fredrik Avatar asked Jun 29 '17 08:06

fredrik


People also ask

How do I switch accounts on Gcloud?

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.

Can I have multiple GCP accounts?

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.


1 Answers

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 
like image 124
Zachary Newman Avatar answered Nov 12 '22 02:11

Zachary Newman