Configuring Google Cloud SDK for multiple projects

gcloud is program which provides command line interface to Google Cloud Platform products and services. So basically everything that is possible to do via Web at console.cloud.google.com it's more or less possible to do with gcloud: deploying Google App Engine applications, create Google Compute Engine instances, hard disks, containers etc. gcloud is part of Google Cloud SDK which consists of several more programs and libraries. In my experience, since I am working on multiple Google Cloud projects and I use often gcloud and it wasn't obvious how to switch between projects. Fortunatelly gcloud provides possibilities to create configurations with separate settings. In this article I want to describe:

  1. process of installing Google Cloud SDK on linux operating system
  2. setting up gcloud and account
  3. using gcloud for multiple configurations

Installing Google Cloud SDK:

On link https://cloud.google.com/sdk/docs/quickstart-linux there are instruction how to install SDK on linux machine. I was doing this on Ubuntu Virtual machine.

first we download SDK installation file, for latest release 155.0.0 at the moment:

wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz

then unpack it 

tar -zxvf google-cloud-sdk-155.0.0-linux-x86_64.tar.gz

go to unpacked folder and start installation process:

cd google-cloud-sdk

./install.sh

we can see following output in console:

Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)?  Y

After confirming

Your current Cloud SDK version is: 155.0.0
The latest available version is: 155.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                  Components                                                 │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│     Status    │                         Name                         │            ID            │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go            │  97.0 MiB │
│ Not Installed │ Bigtable Command Line Tool                           │ cbt                      │   3.9 MiB │
│ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  15.4 MiB │
│ Not Installed │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  21.0 MiB │
│ Not Installed │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   2.3 MiB │
│ Not Installed │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 128.6 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python        │   6.1 MiB │
│ Not Installed │ kubectl                                              │ kubectl                  │  14.9 MiB │
│ Installed     │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Installed     │ Cloud SDK Core Libraries                             │ core                     │   6.0 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │   2.9 MiB │
│ Installed     │ Default set of gcloud commands                       │ gcloud                   │           │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [155.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [155.0.0], run:
  $ gcloud components update


Modify profile to update your $PATH and enable shell command 
completion?

Do you want to continue (Y/n)?  Y

This is the list of available components in Cloud SDK (not everything is installed by default). These can be installed afterwards.

After confirmation we can define rc file which willl be updated, it tries to guess:

The Google Cloud SDK installer will now prompt you to update an rc 
file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use 
[/home/user/.bashrc]:


[/home/user/.bashrc] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

Google Cloud SDK is now installed and ready to use, but you to open NEW console.

 

Setting up gcloud 

After installation, next step is to connect gcloud to your Google account. You can do this with this command:

user@user-VirtualBox:~$ gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                            
Reachability Check passed.
Network diagnostic (1/1 checks) passed.

At this moment window of browser should be opened and you should be prompted to login to your Google account. If that not works, alternative should be to copy link from console and paste manually into browser. If you are successfully authenticated, you will be prompted in your browser to grant permission for Google Cloud SDK:

  • View and manage your applications deployed on Google App Engine
  • View and manage your Google Compute Engine resources
  • View and manage your data across Google Cloud Platform services

If you agree, following lines will be printed into console:

You are logged in as: [[email protected]].

Pick cloud project to use: 
 [1] adventures-on-gcp
 [2] zdenky-udacity
 [3] Create a new project

 Please enter numeric choice or text value (must exactly match list) 

You can pink to use one of the projects which you are already participating in, or you can create completely new (I shortened my list). I'll type 1 in my case

Your current project has been set to: [adventures-on-gcp].

Your project default Compute Engine zone has been set to [us-central1-a].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [us-central1].
You can change it by running [gcloud config set compute/region NAME].

Created a default .boto configuration file at [/home/user/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use [email protected] by default
* Commands will reference project `adventures-on-gcp` by default
* Compute Engine commands will use region `us-central1` by default
* Compute Engine commands will use zone `us-central1-a` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic -h` to learn about advanced features of the SDK like arg files and output formatting

It creates and prints gcloud configuration. I'm not sure based on what it set Compute Engine region and zone, either it uses as default values or maybe based on my settings from other computer. Anyway with following command we can see current project settings on local computer.

user@user-VirtualBox:~$ gcloud config list
[compute]
region = us-central1
zone = us-central1-a
[core]
account = [email protected]
disable_usage_reporting = False
project = adventures-on-gcp

Your active configuration is: [default]

As it's displayed in config, this is named default configuration. We can have multiple configurations (per project for example).

 

Setting multiple configurations

We can see list of all existing configurations on our computer with command:

user@user-VirtualBox:~$ gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT            PROJECT            DEFAULT_ZONE   DEFAULT_REGION
default  True       [email protected]  adventures-on-gcp  us-central1-a  us-central1

So far, I have only one (default) configuration.

To create new configuration, which I will call test-config, I'll type following command:

user@user-VirtualBox:~$ gcloud config configurations create test-config
Created [test-config].
Activated [test-config].

New configuration is created and activated (switched to this configuration). With gcloud init command, we will configure settings for this configuration

user@user-VirtualBox:~$ gcloud init
Welcome! This command will take you through the configuration of gcloud. Settings from your current configuration [test-config] are: [core] disable_usage_reporting = False Your active configuration is: [test-config] Pick configuration to use: [1] Re-initialize this configuration [test-config] with new settings [2] Create a new configuration [3] Switch to and re-initialize existing configuration: [default] Please enter your numeric choice: 1

As you can see, settings are empty, so I will select 1 in order to re-initialize new configuration.

Your current configuration has been set to: [test-config]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                                                           
Reachability Check passed.
Network diagnostic (1/1 checks) passed.

Choose the account you would like to use to perform operations for 
this configuration:
 [1] [email protected]
 [2] Log in with a new account
Please enter your numeric choice:

This is similar process as with initial setup, in this case I can select if I want to use already authenticated account, or I can use different. This is great feature in case you are using several accounts for Google Cloud Platform, this way you can make configurations to switch between multiple accounts. Since I am using only one currently, I'll select first option.

You are logged in as: [[email protected]].

Pick cloud project to use: 
 [1] adventures-on-gcp
 [2] zdenky-udacity
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  2

Again I have possibility to select one of existing projects or create new. I'll select option 2. Valid would be also option 1, even if I am already using it in default configuration. To have multiple configurations for the same project would make sense if you want to use different settings (which you would not want to type as parameters in command). So far I didn't need this case so I can't say how much is efficient.

Next (optional) step in configuration is to set Compute Engine zone/region.

Your current project has been set to: [zdenky-udacity].

Do you want to configure Google Compute Engine 
(https://cloud.google.com/compute) settings (Y/n)?

If I say yes, available zones for selection will be displayed. I selected option 15

Which Google Compute Engine zone would you like to use as project 
default?
If you do not specify a zone via a command line flag while working 
with Compute Engine resources, the default is assumed.
 [1] asia-east1-b
 [2] asia-east1-c
 [3] asia-east1-a
 [4] asia-northeast1-a
 [5] asia-northeast1-c
 [6] asia-northeast1-b
 [7] asia-southeast1-b
 [8] asia-southeast1-a
 [9] europe-west1-c
 [10] europe-west1-b
 [11] europe-west1-d
 [12] us-central1-a
 [13] us-central1-b
 [14] us-central1-c
 [15] us-central1-f
 [16] us-east1-c
 [17] us-east1-b
 [18] us-east1-d
 [19] us-east4-b
 [20] us-east4-c
 [21] us-east4-a
 [22] us-west1-a
 [23] us-west1-b
 [24] Do not set default zone
Please enter numeric choice or text value (must exactly match list 
item): 15
our project default Compute Engine zone has been set to [us-central1-f].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [us-central1].
You can change it by running [gcloud config set compute/region NAME].

Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use [email protected] by default
* Commands will reference project `zdenky-udacity` by default
* Compute Engine commands will use region `us-central1` by default
* Compute Engine commands will use zone `us-central1-f` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [test-config]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic -h` to learn about advanced features of the SDK like arg files and output formatting

So this configuration is set. To check I can type command

user@user-VirtualBox:~$ gcloud config list
[compute]
region = us-central1
zone = us-central1-f
[core]
account = [email protected]
disable_usage_reporting = False
project = zdenky-udacity

Your active configuration is: [test-config]

So now when I list configurations, I see list both with settings. 

user@user-VirtualBox:~$ gcloud config configurations list
NAME         IS_ACTIVE  ACCOUNT            PROJECT            DEFAULT_ZONE   DEFAULT_REGION
default      False      [email protected]  adventures-on-gcp  us-central1-a  us-central1
test-config  True       [email protected]  zdenky-udacity     us-central1-f  us-central1

To switch again to default configuration:

user@user-VirtualBox:~$ gcloud config configurations activate default
Activated [default].

To delete second configuration: 

user@user-VirtualBox:~$ gcloud config configurations delete test-config
The following configurations will be deleted:
 - test-config
Do you want to continue (Y/n)?  Y

Deleted [test-config].

I guess that's all important to know for work with gcloud and multiple projects (configurations) 

Here is video of whole process

blog comments powered by Disqus