How to schedule serverless applications on Google Cloud Platform

15 Feb, 2020
Xebia Background Header Wave

Google Cloud Run is an easy way to deploy your serverless containerized applications. If you need to run your application periodically, you can use Google Cloud Scheduler to do so. In this blog I will show you how to configure a schedule for a serverless application using Terraform.
To invoke serverless applications on a schedule, you need to:

  1. create a service account
  2. deploy your serverless application
  3. grant the scheduler permission
  4. create a schedule
    As I promote the principle of infrastructure as code, I will show you how to do this using terraform code snippets.

Create a service account

You can create a service account for the scheduler with which to invoke the application as follows:

resource "google_service_account" "scheduler" {
  display_name = "Google Cloud Scheduler"
  account_id   = "scheduler"

Deploy using your serverless application

Next you can deploy your serverless application:

resource "google_cloud_run_service" "application" {
  name     = "application"
  location = "europe-west1"

  template {
    spec {
      containers {
        image = ""
  timeouts {
    create = "10m"
  depends_on []

in this case, the applicaton deployed is my trusty paas-monitor application.

Grant the scheduler permission

After the application is deploy, you grant the service account permission to invoke it:

resource "google_cloud_run_service_iam_member" "scheduler-run-invoker" {
  role   = "roles/run.invoker"
  member = "serviceAccount:${}"

  service  =
  location = google_cloud_run_service.application.location

Create a schedule

Finally you create a schedule to invoke the service as follows:

resource "google_cloud_scheduler_job" "application" {
  name        = "application"
  description = "invoke every 5 minutes"
  schedule    = "*/5 * * * *" #<-- Cron expression

  http_target {
    http_method = "GET"
    uri         = "${google_cloud_run_service.application.status[0].url}/status"
    oidc_token {
      service_account_email =
  depends_on = []

The schedule is defined using a cron expression. Note
that Google Cloud Scheduler requires an app engine application to be deployed:

resource "google_app_engine_application" "app" {
  project     = data.google_project.current.project_id
  location_id = "europe-west"
  depends_on  = [google_project_service.appengine]


If you want to deploy the complete example, download and type:

export TF_VAR_project=$(gcloud config get-value project)
terraform init
terraform apply


With Google Cloud Run it is easy to deploy your containerized applications in a serverless fashion and
use the Google Cloud Scheduler to schedule invocation of application in a secure way.

Mark van Holsteijn
Mark van Holsteijn is a senior software systems architect at Xebia Cloud-native solutions. He is passionate about removing waste in the software delivery process and keeping things clear and simple.

Get in touch with us to learn more about the subject and related solutions

Explore related posts