Tuesday, June 7, 2022

Use event subscriptions for scheduled scale up and down of your IBM Cloud Code Engine apps

Few lines of scripting
Over the past months, I wrote several blog posts related to IBM Cloud Code Engine. Code Engine is a managed, serverless platform for containerized workloads. You can run (web) apps and (batch) jobs, both types can receive events. Code Engine apps have settings for minimum and maximum scaling and have assigned compute resources within these boundaries. A "scale to zero" is possible, i.e., there isn't any app instance running and, because it is pay per use, nothing is charged during that time. Which brings up the question, how can I schedule different min / max scale values, e.g., scale down to zero over night, but have some higher minimums during the day? 

I heard that built-in support is on the roadmap. For now, I am using a Code Engine cron subscription for the job...

Scale up and down, scheduled

To change the configured values for the minimum and maximum scaling, it requires to update the deployed Code Engine app and specify the new values. This can be easily done on the command line (see screenshot above). But how to schedule such changes? It is similar to my data scraping project which I blogged about: Use Code Engine cron subscriptions to invoke a job with a simple script.

A secret? Environment variables!

I am using environment variables to parameterize the script for different Code Engine apps and configurations. Those variables are defined in a text file which I register as a secret and then make available to the job. Variables can also be set when invocing the job during a so-called jobrun. Passing environment variables from a cron event to the job is a little bit trickier, but doable.

With that, all is in place and I have the blueprint for my scheduled change of Code Engine app configurations, including the scheduled scaling up and down. See the code on GitHub for details and instructions.

If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.