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:
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.
For more information, including how to deploy a Cloud Run function using Terraform, see Deploy a Cloud Run 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