Schedule an HTTP Cloud Run function


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:

  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 Scheduler job triggers your Cloud Run function.

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

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

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

Required roles

To get the permissions that you need to complete the tutorial, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations .

You might also be able to get the required permissions through custom roles or other predefined roles .

Grant the roles

Console

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

    Go to IAM
  2. Select the project.
  3. Click Grant access .
  4. In the New principals field, enter your user identifier. This is typically the Google Account email address that is used to deploy the Cloud Run service.

  5. In the Select a role list, select a role.
  6. To grant additional roles, click Add another role and add each additional role.
  7. Click Save .

gcloud

To grant the required IAM roles to your account on your project:

  
gcloud  
projects  
add-iam-policy-binding  
 PROJECT_ID 
  
 \ 
  
--member = 
 PRINCIPAL 
  
 \ 
  
--role = 
 ROLE 
  

Replace:

  • PROJECT_NUMBER with your Google Cloud project number.
  • PROJECT_ID with your Google Cloud project ID.
  • PRINCIPAL with the account you are adding the binding for. This is typically the Google Account email address that is used to deploy the Cloud Run service.
  • ROLE with the role you are adding to the deployer account.

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

    Go to Cloud Run

  2. Click Write a function.

  3. In the Configuresection, do the following:

    1. In the Service namefield, enter a name for your function.
    2. In the Regionlist, select a region.
    3. Don't modify the default runtime language.
    4. 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 .

  4. Accept the other defaults and click Create.

  5. 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 Schedulerpage.

    Go to Cloud Scheduler

  2. Click Click Create job.

  3. Enter a name for your job.

  4. In the Regionlist, select a region.

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

      30 
      
     16 
      
     * 
      
     * 
      
     7 
     
    

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

  6. In the Timezonelist, select a time zone.

  7. Click Continue.

  8. In the Target typelist, select HTTP.

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

  10. In the Auth headerlist, select Add OIDC token.

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

  12. In the Audiencefield, 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 executionshould 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 Runpage.

    Go to Cloud Run

  2. Select Filter services > Deployment type > Function. This filters the list of services to only show functions.

  3. Click the function name.

    The Service detailspage opens and displays service metrics.

  4. Click the Logstab.

    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

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

    Go to Cloud Scheduler

  2. Select the checkbox next to your job.

  3. Click Deleteand confirm your deletion.

  4. In the Google Cloud console, go to the Cloud Runpage.

    Go to Cloud Run

  5. Select the checkbox next to your service.

  6. Click Deleteand confirm your deletion.

  7. In the Google Cloud console, go to the Service Accountspage.

    Go to Service Accounts

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

  9. Click Deleteand confirm your deletion.

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: