This tutorial shows how to schedule an HTTP Cloud Run function by targeting an HTTP endpoint in Cloud Scheduler. Use an HTTP function when you need your function to have a URL endpoint and respond to HTTP requests, such as for webhooks.
Alternatively, if you created a CloudEvents function, use Eventarc event triggers to schedule your function to respond to events within your project. Eventarc can trigger messages on a Pub/Sub topic, changes in a Cloud Storage bucket, and more. For more information, see Types of Cloud Run functions.
Scheduling the execution of a Cloud Run function is a common use case for Cloud Scheduler. In this tutorial, you will:
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
Note that you must grant the Cloud Run Invoker role because in Cloud Run functions, invocation permissions are available by managing the underlying Cloud Run service.
Create an HTTP Cloud Run function
Create an HTTP function that is triggered by Cloud Scheduler.
In the Google Cloud console, go to the Cloud Run page.
Click Write a function.
In the Configure section, do the following:
- In the Service name field, enter a name for your function.
- In the Region list, select a region.
- Don't modify the default runtime language.
For authentication, select Require authentication.
If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.
For more information, see Authenticate for invocation.
Accept the other defaults and click Create.
Copy the HTTP URL for the function.
Create a Cloud Scheduler job
Create a Cloud Scheduler job that triggers an action using HTTP.
In the Google Cloud console, go to the Cloud Scheduler page.
Click Click
Create job.Enter a name for your job.
In the Region list, select a region.
Specify the frequency for your job, using a unix-cron format:
30 16 * * 7
For more information, see Cron job format and time zone.
In the Timezone list, select a time zone.
Click Continue.
In the Target type list, select HTTP.
In the URL field, enter the function's HTTP URL that you previously copied.
In the Auth header list, select Add OIDC token.
In the Service account list, select the service account you previously created.
In the Audience field, enter the function's HTTP URL that you previously copied.
Click Create.
You have created a cron job that runs your Cloud Run function at 16:30 on Sundays.
Run your Cloud Scheduler job
You can now run the job you created.
In the Google Cloud console, go to the Cloud Scheduler page.
Select the checkbox for the job that you created and click Force run.
When it is initially invoked, the first job created in a project can take a few minutes to configure and run.
After the job runs, the Status of last execution should indicate
Success
.
Verify the results in Cloud Run functions
You can verify that your Cloud Run function is successfully triggered and executed by your cron job.
In the Google Cloud console, go to the Cloud Run page.
Select Filter services > Deployment type > Function. This filters the list of services to only show functions.
Click the function name.
The Service details page opens and displays service metrics.
Click the Logs tab.
You should see a log entry similar to
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete project
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete tutorial resources
In the Google Cloud console, go to the Cloud Scheduler page.
Select the checkbox next to your job.
Click
Delete and confirm your deletion.In the Google Cloud console, go to the Cloud Run page.
Select the checkbox next to your service.
Click
Delete and confirm your deletion.In the Google Cloud console, go to the Service Accounts page.
Select the checkbox next to the service account you created.
Click
Delete and confirm your deletion.
What's next
- Use Cloud Scheduler to trigger Cloud Run on a schedule
- Write CloudEvents functions to schedule your function to respond to events within your project.