☁️ customer engineer at google
🏂 snowboarder
🍞 carb addict
🏘 real estate investor
💃🏻 dancer
✈️ traveler
📖 learner
👾 ex-gamer with semi-annual relapses
☕️ fueled by caffeine

linkedin github

Context Switching With Gcloud Cli

17 Nov 2021

Working as a cloud or devops engineer for a consulting company means you do a lot of context switching. Specifically, switching between your various AWS profiles, GCP projects, or Azure tenants. You’ve got different accounts under your main company such as your personal sandbox, your coworker’s sandbox, the main business infrastructure project, etc. You’ve got multiple projects for your variety of customers (past and present), and then of course there are different isolated environments for each one. The list goes on!

This post aims to showcase how I handle the creation and context switching of my Google Cloud Platform projects via gcloud.


For anyone who’s worked in GCP, there are multiple commands you need to run in order to get started. Some are long and difficult to remember. There is the option to run gcloud init. It’ll run through an interactive setup which’ll authorize gcloud to access GCP with the credentials you pass in, as well as set up a new configuration or update an existing one. But that’s not something you would want to run every time you need to create a project and set its corresponding properties. For the non-interactive setup, you’d run the following commands for each new configuration:

gcloud config configurations create $CONFIG_NAME
gcloud config configurations activate $CONFIG_NAME
gcloud auth login
gcloud config set project $PROJECT_NAME
gcloud config set compute/zone $ZONE

Some masochist or engineer who enjoys toil would be running gcloud --help, or scrolling up on their shell’s command history, or googling for the commands every single time they needed to set up a new configuration. Or maybe not if they memorized these commands after running them the first time! But my memory is just a smidge better than a goldfish’s and I revel in enough pain at work as is. So let me introduce my makefile!  # set default variable

	gcloud config configurations create $(GCP_PROJECT_ID) && gcloud config set project $(GCP_PROJECT_ID) && gcloud config set account $(EMAIL)
	# example command: make gcc GCP_PROJECT_ID=bananabread

Now instead of those 5 long commands, a new project creation would require just this:


One command to create them all! 😊

After this initial setup, the process only requires:

  1. typing make in your terminal
  2. doing a reverse search of your command history
  3. passing in the GCP project name!


Alright, so switching configurations normally looks a bit like this:

gcloud config configurations list
gcloud config configurations activate $PROJECT_ID

Not as bad as creating but I know plenty of engineers who would struggle to remember and accurately type this out. I see you guys out there running your commands with all your typos and wondering why it didn’t execute properly!! 👀

There are 2 ways you can switch configurations more easily. You can use environment variables or aliases.

1. Environment Variables

Set your default configuration in your shell’s configuration file.


Use a different configuration and override the default by exporting a different value for the variable on the command line! I typically just begin typing out export CLOUD and scroll up in my command history until I find the project I’m looking for.

2. Aliases

I’m sure you know about aliases but if you didn’t set this up already, here are what mine look like:

# GCloud
alias gccl="gcloud config configurations list"
alias gcca="gcloud config configurations activate"
alias gccd="gcloud config configurations delete"
alias gcal="gcloud auth list"
alias gal="gcloud auth login"
alias gaal="gcloud auth application-default login"

So now, switching configurations should look like this:


I’m always trying to improve my workflows. If there’s a better way to do these things or if you ended up incorporating this into your own workflow, let me know! I hope I’ve saved you some time and pain and I’ll see you in the next post!