Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

How to switch between multiple accounts and projects in Google Cloud SDK (gcloud)

We added a new project to Google Cloud Platform and set up the Google Cloud SDK (gcloud CLI) to switch projects.

Once you set it up, you don't have to tweak it, and if you don't, you'll forget how to do it, so I'll write down how to set it up for myself.

Required Information

The following four pieces of information are required to execute the gcloud command.

  • account
  • Project
  • region
  • zone

Region and zone are not required, but they are also asked in the gcloud initialization command gcloud init, so they should be set.

gcloud keeps a set of these configuration values as a single Config, and when the gcloud command is executed, the values are retrieved from that Config and applied.

So, switching between multiple accounts/projects can be done by rewriting the account/project in that Config.

However, since gcloud has the ability to define multiple configurations and switch between them, the method used here is to create as many configurations as you want to switch between, set up an account/project in each config, and switch between the active configs to switch between multiple accounts/projects. We used the method of switching multiple accounts/projects by creating as many configs as you want to switch, setting an account/project in each config, and switching the active config.

Account Registration

Register an account with gcloud.

There are two types of GCP accounts, "user accounts" and "service accounts," and the differences between them and how to register with gcloud are described below.

  • user account

    • So-called Google account.
    • Register as gcloud auth login
  • service account

    • New accounts that can be added arbitrarily in GCP's IAM
    • Register as gcloud auth activate-service-account --key-file=[KEY_FILE}

User accounts, as well as service accounts, can access other projects across projects.

An explanation of this area is provided below.

www.ekwbtblog.com

When you register an account with gcloud, your account credentials will be copied under ~/config/gcloud, and you will be able to access your project from gcloud without login or key file.

Account List

gcloud auth list

Add Config

Initially there is only a "default" Config, so add a Config for switching.

gcloud config configurations create [Config名]

Config List

gcloud config configurations list

Switch to active Config

gcloud config configurations activate [Config名]

Config setting

Set the four aforementioned information "Account," "Project," "Region," and "Zone" in the added Config.

Activate the Config you wish to set and execute the following command.

gcloud config set account [アカウント名]
gcloud config set project [プロジェクトID]
gcloud config set compute/region [リージョン名]
gcloud config set compute/zone [リージョン名]

Click here for a list of regions and zones

Config setting confirmation

gcloud config list

Delete Config

Deactivate the Config you wish to delete and perform the following

gcloud config configurations delete [Config名]

User Account Switching

Now let's get to the main issue of switching users/accounts.

Method 1) Switch active Config.

Switch to the active Config and execute the gcloud command.

gcloud config configurations activate [Config名]

Method 2) Specify Config.

You can also use the gcloud command to specify the Config to be used.

gcloud [コマンド] --configuration=[Config名]

Method 3) Automatically switch Config.

Config can also be specified with the environment variable "CLOUDSDK_ACTIVE_CONFIG_NAME".

It is easy to switch Config and forget and edit a different project, so as Google docs, So, I used direnv to change the environment variable "CLOUDSDK_ACTIVE_CONFIG_NAME" for each folder, so that the Config can be switched automatically.

direnv installation

sudo apt-get install direnv

.bashrc

eval "$(direnv hook bash)"

direnv configuration

If the environment variable "CLOUDSDK_ACTIVE_CONFIG_NAME" is entered in the ".envrc" file, the folder under that folder will become the specified Config.

.envrc

export CLOUDSDK_ACTIVE_CONFIG_NAME=[Config名]

Location of gcloud configuration information storage

Account authentication information, config data, and other gcloud configuration information is stored in ~/.config/gcloud, If you want to bring your gcloud settings to another PC, you can do so by copying this folder.

Impressions, etc.

I often forget to set the project name in docker-compose, but it seems that "COMPOSE_PROJECT_NAME" can be set automatically. I often forget to set the project name in docker-compose.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com