Scheduling GCP Cloud Functions

Do you want to schedule code you’ve written in a Google Cloud Function to run at a certain time of day? Read on!

Google does not make it easy to figure out how to schedule jobs that run automatically on some consistent basis but there is now a Beta available that will allow you to do just that!

First log into your GCP Console and search for “Cloud Scheduler”.

After you’ve enabled Cloud Scheduler then you can create a new scheduled job. Give it a unique name and a description. The frequency is in “unix-cron format”. My purposes just require daily schedule that runs at 7AM. So if you want to schedule a daily job then use this as the Frequency 0 7 * * * where the “7” is whatever hour of the day you want (i.e. 3PM is 15). For more info on cron scheduling head here.

For the target you can select HTTP. If you created a Google Cloud Function with an HTTP trigger this is what you would select. If you created one with Pub/Sub then you would select that!

After that you can select the HTTP method as well as a body if you select “POST”. This is one possible way to control access to the trigger by having the Cloud Scheduler send some sort of token that your cloud function then validates. This will prevent other people from being able to trigger your cloud function by just typing in the HTTP trigger on any browser.

Happy scheduling!