Scheduling a workflow using Cloud Scheduler

This page shows you how to use Cloud Scheduler to run a workflow on a particular schedule, such as every Monday at 9 AM or every 15 minutes.

Before you begin

  1. If you do not already have a workflow that you wish to schedule, create one.
  2. Enable the Cloud Scheduler API.

    Enable the API

Scheduling a workflow

  1. In order for Cloud Scheduler to make requests to the Workflows API, you must first create a service account:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

    Where SERVICE_ACCOUNT_NAME is the name you want to give to your new service.

  2. Grant the account that will run your Cloud Scheduler commands permission to use your service account.

  3. Grant your new service account the workflows.invoker role so that the account has permission to trigger your workflow:

    gcloud projects add-iam-policy-binding PROJECT_NAME \
      --member serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_NAME.iam.gserviceaccount.com \
      --role roles/workflows.invoker

    Where:

    • PROJECT_NAME is the name of your Google Cloud project.
    • SERVICE_ACCOUNT_NAME is the name of the service account that you previously created.
  4. Create a Cloud Scheduler job that triggers your workflow, using the service account you previously created to authenticate. For example, to schedule your workflow to execute every 5 minutes (defining the interval using unix-cron format):

    gcloud beta scheduler jobs create http JOB_NAME \
      --schedule="*/5 * * * *" \
      --uri="https://workflowexecutions.googleapis.com/v1/projects/PROJECT_NAME/locations/REGION_NAME/workflows/WORKFLOW_NAME/executions" \
      --time-zone="TIME_ZONE" \
      --oauth-service-account-email="SERVICE_ACCOUNT_NAME@PROJECT_NAME.iam.gserviceaccount.com"

    Where:

    • JOB_NAME is the name you are giving to your Cloud Scheduler job.
    • PROJECT_NAME is the name of your Google Cloud project.
    • REGION_NAME is the region your Workflow is in, such as us-central1.
    • WORKFLOW_NAME is the name of the workflow you want to schedule execution of.
    • TIME_ZONE is the time zone that Cloud Scheduler should use to interpret the schedule you provide.
    • SERVICE_ACCOUNT_NAME is the name of the service account that you previously created.

    Your workflow now executes every 5 minutes.

What's next