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
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!
EMAILfirstname.lastname@example.org # set default variable gcc: 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 EMAILemail@example.com
Now instead of those 5 long commands, a new project creation would require just this:
make gcc GCP_PROJECT_ID=$PROJECT --directory $PATH_TO_MAKEFILE
makefiledirectory every time you run this command, pass in the directory and this command will be totally reusable for years to come!
makefileor you can pass in a different one by adding
One command to create them all! 😊
After this initial setup, the process only requires:
makein your terminal
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.
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.
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:
gccl gcca $PROJECT_ID
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!