This page describes how to execute Cloud Run jobs on a schedule using Cloud Scheduler.
Required roles
To get the permissions that you need for the operations described on this page, ask your administrator to grant you the IAM roles on your Cloud Run job:
- Cloud Scheduler Admin
(
roles/cloudscheduler.admin), or a custom role with thecloudscheduler.jobs.createpermission. - Cloud Run Invoker (
roles/run.invoker) to execute jobs using the Google Cloud CLI, or Cloud Run Developer (roles/run.developer) to execute jobs using the Google Cloud console.
For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run job interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.
Before you begin
- Create a Cloud Run job if you have not created one yet.
Enable the Cloud Scheduler API for your project:
gcloud services enable cloudscheduler.googleapis.com
Configure a Cloud Run job to execute on a schedule
You can configure a Cloud Run job to execute on a schedule using the Google Cloud console, the gcloud CLI, or Terraform:
Console
In the Google Cloud console, go to the Cloud Run jobs page:
Click the job you want to execute on a schedule.
Click the Triggers tab.
Click Add Scheduler Trigger.
In the Cloud Scheduler job form, follow these steps:
For Name, enter a name for your Cloud Scheduler job.
For Region, select a region for your Cloud Scheduler job. It does not need to match the region used for the Cloud Run job.
Specify the frequency for your job execution, using the unix-cron format, for example,
0 12 * * *Select your Timezone.
Click Continue.
In the Configure the execution section, select the service account that has permission to invoke the current Cloud Run service.
Click Create to create the Cloud Scheduler job that will execute the Cloud Run job at the specified frequency.
gcloud
Run the following command:
gcloud scheduler jobs create http SCHEDULER_JOB_NAME \
--location SCHEDULER_REGION \
--schedule="SCHEDULE" \
--uri="https://run.googleapis.com/v2/projects/PROJECT_ID/locations/CLOUD_RUN_REGION/jobs/JOB:run" \
--http-method POST \
--oauth-service-account-email PROJECT_NUMBER-compute@developer.gserviceaccount.com
Replace the following:
SCHEDULER_JOB_NAME: the name you want to give your scheduler job.SCHEDULER_REGION: a region supported by Cloud Scheduler—for example,europe-west2.SCHEDULE: the necessary frequency, for example0 12 * * *.PROJECT_ID: your project ID.CLOUD_RUN_REGION: the region for your Cloud Run job. For example,europe-west1.JOB: the name of your Cloud Run job.PROJECT_NUMBER: your project number.
Terraform
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
Add the following to agoogle_cloud_run_v2_job
resource in your Terraform configuration:Cloud Scheduler will execute the Cloud Run job at the specified frequency.
Schedule your job inside a VPC Service Controls perimeter
If your Google Cloud project is inside a VPC Service Controls perimeter in a Virtual Private Cloud network, the Cloud Scheduler integration with VPC Service Controls supports Cloud Run jobs as a target using the following URI format:
https://run.googleapis.com/v2/projects/PROJECT_ID/locations/REGION/jobs/JOB:run
Replace the following:
PROJECT_ID: the Google Cloud project ID.REGION: the Google Cloud region of the job.JOB: the name of your Cloud Run job.
For more information on configuring VPC Service Controls for Cloud Run, see Using VPC Service Controls (VPC SC). For more information on using Cloud Scheduler with VPC Service Controls, see Secure cron jobs with VPC Service Controls.
What's next
After you use this feature, you can do the following: