This tutorial shows you how to trigger an HTTP Cloud Run function using Cloud Scheduler and by targeting an HTTP endpoint. Use an HTTP function when you need your function to have a URL endpoint and respond to HTTP requests, such as for webhooks.
Note that you can also schedule an event-driven function when your function should be triggered directly in response to events within your Google Cloud project, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket. 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 a simple HTTP Cloud Run function.
- Create a Cloud Scheduler job.
- Run the Cloud Scheduler job.
- Verify that the Cloud Run function was triggered by the Cloud Scheduler job.
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 Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- 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 Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
- By default, Cloud Run functions uses the automatically created
default Compute Engine service account
for its runtime service account. You can use this service account to try out
this tutorial. However, depending on your organization policy configuration, the
default service account might not automatically be granted the Editor role on
your project. If that is the case, you must
grant the service account
the following roles:
- Artifact Registry Writer (
roles/artifactregistry.writer
) - Logs Writer (
roles/logging.logWriter
) - Storage Object Viewer (
roles/storage.objectViewer
)
- Artifact Registry Writer (
Note that you must grant the Cloud Run Invoker role because in Cloud Run functions (2nd gen), 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 functions page.
Click
Create function.In the Basics section, do the following:
- In the Environment list, select 2nd gen.
- Enter a name for your function.
- In the Region list, select a region.
In the Trigger section, do the following:
- In the Trigger type list, select HTTPS.
For authentication, select Require authentication.
For more information, see Authenticate for invocation (2nd gen).
Accept the other defaults and click Next.
Don't modify the default runtime language and sample code. The code does not use any cloud services and does not need additional permissions enabled. Click Deploy.
Once the function is successfully deployed, click the Trigger tab.
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 functions page.
Click the function name.
The Function details page opens and the Invocations/Second graph shows your first invocation of the function.
Click the Logs tab.
You should see a log entry similar to
POST 200 146 B 5 ms Google-Cloud-Scheduler https://REGION-PROJECT_ID.cloudfunctions.net/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 functions page.
Select the checkbox next to your function.
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.