YURKOL Ltd - Custom Software and Cloud Architectural Solutions for Modern Businesses

Switching active gcloud configuration.

gcloud CLI (Command Line Interface) is a time saving command-line utility provided by Google Cloud Platform (GCP) that allows us to interact with various GCP services and manage resources from the command line. It provides a set of commands and options that we can use to create, configure, and manage cloud resources.

Of course, we always work on multiple GCP projects that reside on our machines and gcloud allows us to interact with all the resources and services which belong to different GCP projects (often in different organizations). It is achieved with gcloud configurations, where each configuration corresponds to a given project.

All locally available gcloud configurations can be listed with the following command...

yurii@localhost:~> gcloud config configurations list

... and output will be similar to this:
    
NAME        IS_ACTIVE  ACCOUNT        PROJECT            COMPUTE_DEFAULT_ZONE  COMPUTE_DEFAULT_REGION
project-1   True       me@yurkol.com  project1-391705                          europe-west4
project-2   False      me@yurkol.com  project2-170592                          europe-central2
    
    

As we can see, the configuration with the name project-1 is set to be ACTIVE. All the commands issued to gcloud will be applied to the project named project1-391705.

Changing active configurations is also pretty straightforward:

yurii@localhost:~> gcloud config configurations activate project-2

Looks very simple. But usually different projects have similar layout and file and directories structure (according to best practices, yes). Moreover, we seldom use gcloud utility by directly typing in its commands - in practice, especially when CI/CD is already set, all those commands are used inside Makefile targets, bash scripts etc. And this is when problems might arise: say you forgot to switch active gcloud configuration and execute your make target in full confidence that you're working with the right project (when you are indeed not).

Using direnv

What configuration gcloud utility considers as active is governed by the CLOUDSDK_ACTIVE_CONFIG_NAME environment variable. And in *nix operating systems (Linux & MacOS) we can set different value to this (and any other) environment variable depending on current working directory. This is when direnv really comes in handy. Every Linux distro is shipped with this utility, for example in OpenSUSE it is installed with

yurii@localhost:~> sudo zypper in direnv

Also we need to hook direnv to our terminal. To do that, add the following line to your ~/.bashrc:

eval "$(direnv hook bash)"

Don't forget to re-login in order the changes to take place.

After that navigate to the root directory of your project (or to the directory with CI/CD stuff inside this project). And create an .envrc file with this content:

export CLOUDSDK_ACTIVE_CONFIG_NAME=project-1

Upon saving this file, we'll get the message from direnv:

direnv: error /home/yurii/project-1/.envrc is blocked. Run `direnv allow` to approve its content

Run direnv allow to activate this .envrc
Repeat this with the rest of your GCP project dirs.

From now on, whenever you're navigating to a directory with such an .envrc file, you can be sure that all the commands intended for a project located in this directory won't be fired against any other project, no matter which one is set as ACTIVE globally; you will never kill Alice's cloud run service, being sure that you're working with Bob's project. Feel free to set any other gcloud properties using direnv.