Schedule an HTTP Cloud Run function


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:

  1. Create a simple HTTP Cloud Run function.
  2. Create a Cloud Scheduler job.
  3. Run the Cloud Scheduler job.
  4. 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. New Google Cloud users might be eligible for a free trial.

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  10. 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.

  11. 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:
    1. Artifact Registry Writer (roles/artifactregistry.writer)
    2. Logs Writer (roles/logging.logWriter)
    3. Storage Object Viewer (roles/storage.objectViewer)

Create an HTTP Cloud Run function

Create an HTTP function that is triggered by Cloud Scheduler.

  1. In the Google Cloud console, go to the Cloud Run functions page.

    Go to Cloud Run functions

  2. Click Create function.

  3. In the Basics section, do the following:

    1. In the Environment list, select Cloud Run function.
    2. Enter a name for your function.
    3. In the Region list, select a region.
  4. In the Trigger section, do the following:

    1. In the Trigger type list, select HTTPS.
    2. For authentication, select Require authentication.

      For more information, see Authenticate for invocation (2nd gen).

  5. Accept the other defaults and click Next.

  6. 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.

  7. Once the function is successfully deployed, click the Trigger tab.

  8. Copy the HTTP URL for the function.

Create a Cloud Scheduler job

Create a Cloud Scheduler job that triggers an action using HTTP.

  1. In the Google Cloud console, go to the Cloud Scheduler page.

    Go to Cloud Scheduler

  2. Click Click Create job.

  3. Enter a name for your job.

  4. In the Region list, select a region.

  5. Specify the frequency for your job, using a unix-cron format:

    30 16 * * 7
    

    For more information, see Cron job format and time zone.

  6. In the Timezone list, select a time zone.

  7. Click Continue.

  8. In the Target type list, select HTTP.

  9. In the URL field, enter the function's HTTP URL that you previously copied.

  10. In the Auth header list, select Add OIDC token.

  11. In the Service account list, select the service account you previously created.

  12. In the Audience field, enter the function's HTTP URL that you previously copied.

  13. 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.

  1. In the Google Cloud console, go to the Cloud Scheduler page.

    Go to Cloud Scheduler

  2. 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.

  3. 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.

  1. In the Google Cloud console, go to the Cloud Run functions page.

    Go to Cloud Run functions

  2. Click the function name.

    The Function details page opens and the Invocations/Second graph shows your first invocation of the function.

  3. 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

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete tutorial resources

  1. In the Google Cloud console, go to the Cloud Scheduler page.

    Go to Cloud Scheduler

  2. Select the checkbox next to your job.

  3. Click Delete and confirm your deletion.

  4. In the Google Cloud console, go to the Cloud Run functions page.

    Go to Cloud Run functions

  5. Select the checkbox next to your function.

  6. Click Delete and confirm your deletion.

  7. In the Google Cloud console, go to the Service Accounts page.

    Go to Service Accounts

  8. Select the checkbox next to the service account you created.

  9. Click Delete and confirm your deletion.

What's next