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. Create a service account so that Cloud Scheduler can make requests to the Workflows API:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

    Replace SERVICE_ACCOUNT_NAME with a name that is between 6 and 30 characters. It can contain lowercase alphanumeric characters and dashes. After you create a service account, you cannot change its name.

  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

    Replace the following:

    • 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, schedule your workflow to execute every 5 minutes (defining the interval using unix-cron format):

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

    Replace the following:

    • 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.
    • DOUBLE_ESCAPED_JSON_STRING is a JSON encoding of any arguments you are passing. The double quotation marks inside the quoted string are escaped using backslashes (\). For example: --message-body="{\"argument\": \"{\\\"foo\\\": \\\"bar\\\"}\"}"
    • 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