Optimizing for deployment speed on Google Cloud
Google’s GCP Partner Team recently organized an event called “Partner Skills Challenge”. It consists of a series of gamified challenges built around GCP services. Each week, a major theme is selected and partners’ engineers can compete in “Cloud Hero Challenges”, scoring in a leaderboard. This offered me the opportunity to sharpen some of my scripting and automation skills and also allowed me to learn about some interesting tidbits that you only stumble upon when actually trying to automate deployments in a very specific way.
In these challenges the score is determined both by the correct completion of the tasks at hand, and by the speed completing them: the faster the better.
Each challenge provides credentials to a Qwiklabs environment, with access to a real Google Cloud project (with some guardrails).
To succeed speed is key, and speed REQUIRES automation.
Unfortunately, this year I did not had much time available to invest in the challenges, due to professional and personal projects, so I decided to take the shortest/simplest route, and use the platform native tools. They are very well documented in the extensive Google Cloud documentation. I also considered using Terraform, but gcloud cli is much straightforward and most likely has 100% coverage of all GCP features and nuances… so “gcloud cli” it is!
What follows are some of the things I learned through the process of trying to automate the challenges.
Authentication and quick profile switching
Google’s Cloud “gcloud” cli tool, requires authentication to be done in a very specific way: through a web browser. I considered using automation even for this part, using Selenium (or similar), but only a few seconds would be saved, and finally considered it not to be worth the effort, given the very short allotted time I had for the task. What I ended up using is gcloud’s “configurations” feature, documented here. It is a very convenient and quick way of encapsulating an account/project environment configuration as a complete entity and allowing you to quickly change back a forth between two projects or event between two completely different accounts executing a simple command.
Using it is pretty straightforward:
gcloud config configurations create $PROJECT
gcloud config configurations activate $PROJECT
gcloud config set core/project $PROJECT
gcloud auth login
This will create, activate and login into a configuration named after the project name (contained in env.variable $PROJECT).
The last command will open a web browser to login into GCP console and authorize gcloud’s SDK to access the account using the provided credentials.
Service Account permission scopes and GUI vs. CLI
In one of the challenges, I faced a quirk that helped me refresh my knowledge about Service Accounts on Google Cloud, when used to provide access to other GCP services to an instance.
I created a service account with a set of permissions needed to use BigQuery from an instance. This allows access to the service, without having to authenticate from within the app, and thus avoiding the need for secret management. Nothing fancy, this is exactly the intended use case for service accounts. But then, I observed different behavior depending on the method used to create the instance: the console/GUI created instance was able to access BigQuery with the service account, but the gcloud cli tool created one was failing to query BigQuery. Both were running with the same service account! I provided exactly the same information both through GUI dialog, and through gcloud cli parameters! Debug time!
As usual, the error message provided the hint about what was happening: it complained about some issue related to “Oauth scope”.
Basically, the GUI was being intelligent, and was activating behind the scenes (I did not specify it) the access scope required for BigQuery. I’m guessing it does this when the service account associated with the instance has some permisions outside of the default scope.
On the other hand, my manually crafted gcloud cli command was not explicitly enabling the required –scopes parameter, so only the default scope was enabled.
Simple enough solution, adding the following parameter to the gcloud cli command, solved the problem:
--scopes default,bigquery
Adding multiple roles to a SA with gcloud add-iam-binding
I also quickly discovered that gcloud add-iam-policy-binding does not allow adding more than one role for each command invocation. So depending on the number of roles you need for a specific service account, you have to call the same command in a loop for each one of them… no way around it, except creating a .yaml template with the full definition, of course.
Parallel execution
Thinking about how to improve time-to-completion, parallel execution of commands was the next obvious step. This was possible for some of the challenges that had non-consecutive dependencies on some of the tasks.
On a shell script it is quite easy to tell the shell not to wait until command completion by using the ‘&’ built-in at the end of the command. This will cause the shell to immediately execute the next script line without waiting for the previous one to complete, which will be spawn/executed as a background job.
Once the script reaches a point where the next command requires the existence of an object/entity created on a previous step, you can use the ‘wait’ built-in without any parameters, and this will cause the shell to wait for the complete execution of all previous child background jobs. Pretty straightforward. An example would look something like:
command-1 &
command-2 &
wait
command-3
That would cause the shell to execute command-1 and command-2 in parallel, and wait for completion of both, before executing command-3.
Startup-script for instances
Another pretty useful timesaver trick when creating vm instances, is using a startup-script in order to execute whatever commands you need to execute inside the guest os, once it has booted. Keep in mind that those scripts will be executed as the root user, and that the current working directory will be set to “/”.
To specify the startup-script, gcloud cli offers two methods: Providing the script from a local file with:
--metadata-from-file=startup-script=./script.sh
Or, directly specifying the commands to execute, on the command line parameters:
--metadata=startup-script='apt update; apt -y install apache2'
This only works with instance images that have Compute Engine tools installed (all Google Provided ones). This also works for Windows instances, but you have to use a different metadata key. See the official documentation here.
Closing thoughts
All things considered, quite a good ROI (return of investment) for the time used.
Althought I’m pretty sure the team designing these challenges did not intend them to be completed the way I did, my approach has provided me a better time/learning value ratio out of them. :)
Thank you Google Partner team, for providing this engaging learning environment!