Google Cloud Functions: Scheduling (Cron)

In a previous article, I’ve explained 5 easy steps to deploy Google Cloud Function. In this article, I’ll explain how to set up cron for it on Google Cloud Standard App Engine Platform.

The problem is that NodeJS is only available on App Engine Flexible, but it doesn’t make sense to set up flexible instance just for kicking GCF with cron.

Instead, we can create a simple python app and deploy it on App Engine Standard Platform.

We’ll need to add 3 files to the application:

app.yaml

runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
skip_files:
- ^node_modules/.*

cron.yaml

cron:
- description: "regular job"
url: /hourly
schedule: every 1 hours

main.py

import webapp2
import urllib2
class HourlyCronPage(webapp2.RequestHandler):
def get(self):
response = urllib2.urlopen('<url_of_your_cloud_function>')
self.response.write(response.read())
app = webapp2.WSGIApplication([
('/hourly', HourlyCronPage),
], debug=True)

After creating these files, steps to deploy a cronjob runner are as follow:

gcloud app deploy
gcloud app deploy cron.yaml

Enjoy! 🎉


Hi, I’m Valerii. I live and write in Amsterdam. I wrote this article, all views are my own. If you enjoyed reading it, make sure to follow me on twitter https://twitter.com/viatsko