Seamless switching b/w multiple projects using gcloud configurations
Struggle of switching between multiple GCP projects is actually real. In addition to that, making mistakes like running destrucutive commands in a wrong GCP project is also fairly common.
In this article, we will see how we can utilize gcloud
configurations to seamlessly switch between GCP projects without going back and forth to the GCP console to fetch the project ids and other info.
Note: We can have multiple configurations for a GCP project with different properties/contexts. For the sake of this article I’ll use the term project but that should not limit us only to the GCP projects.
Default configuration
When we install gcloud
a default
configuration is created for us and most engineers from this point don’t bother to configure further. Most of them still always rely on copy/pasting project ids and other info. (I’ll bet on your Notes app 😛) everytime when working with gcloud
.
|
|
Multiple configurations
Good news is - gcloud
supports multiple configurations (kinda similar to the aws
profiles) which makes switching between GCP projects/contexts a seamless experience.
Let’s see them in more detail:
Creating new configurations
|
|
Activating a new configuration
Although the create
command automatically activates the configuration it has just created, but in case we want to switch to a different one you can use the following command.
|
|
Setting properties for the configurations
Now it’s time to set some properties for each configuration which will set the context for a particular configuration.
Tip: To list all the supported properties, use this command $ gcloud config list --all
There are two approaches to set these properties:
- Edit the configuration’s file directly
- Use
gcloud
CLI to update these values
1. Edit the configuration file directly
By default in the *nix systems all these configurations reside in $HOME/.config/gcloud/configurations
.
|
|
We can directly edit these files and add some properties for these configurations in one go.
An example configuration might look like
|
|
Replace these properties with your values in your machine and you’re good to go as soon as you save and quit.
2. Use gcloud
CLI to set the properties
gcloud
cli also offers feature to set/unset these properties using CLI commands.
|
|
You can edit all other configurations in the similar way or even prepare a script to set these programmatically if you have 10s of projects to manage.
Once you’re done, you need to re-login using $ gcloud auth login
.
Switching between configuration
Finally after setting the properties, we are good to go now. To switch between GCP projects, we just need to activate the configuration for that particular project. Once done all the subsequent gcloud
will use that configuration’s properties as their context.
|
|
Apart from the above, most of the gcloud
commands also support --configuration=<name>
.
|
|
Bonus Tip #1
You can use tools like direnv that will automtically set the appropriate ENV var as soon as you cd
into a specific directory.
This is particularly useful when you have IaC directories for each environment.
|
|
direnv
setup would look like
|
|
Now as soon as you change the directories your gcloud
configuration should automatically point to the correct one. direnv
also unsets the env var as soon as you are out from a directory so you don’t need to worry about if you are working in the right context anymore.
Bonus Tip #2
Just for your convience, you can always print what’s your current active configuration in your Shell promopt (or in the tmux’s status bar). This will keep you always aware and might help you avoid executing destructive commands in a wrong project.
|
|
Add the above snippet in your ~/.bashrc
or ~/.bash_profile
to make it always available.
I believe we learned something new today which will definitely be a productivity booster for the daily users of gcloud
and from now on they will be able to smoothly switch between multiple GCP contexts.
Till next time,
#Ciao #HappyHacking