This tutorial shows how to schedule an HTTP Cloud Run function
by targeting an HTTP endpoint in Cloud Scheduler. Use an
[HTTP function](https://docs.cloud.google.com/run/docs/write-functions.md.txt#write-http-functions)
when you need your function to have a URL endpoint and respond to HTTP requests,
such as for webhooks.

Alternatively, if you create an event-driven function based on CloudEvents,
use Eventarc 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
[Write Cloud Run functions](https://docs.cloud.google.com/run/docs/write-functions.md.txt).

Scheduling the execution of a Cloud Run function is a common
use case for Cloud Scheduler. In this tutorial, you will:

1. [Create an HTTP Cloud Run function](https://docs.cloud.google.com/scheduler/docs/tut-gcf-http.md.txt#create-function).
2. [Create a Cloud Scheduler job](https://docs.cloud.google.com/scheduler/docs/tut-gcf-http.md.txt#create-scheduler-job).
3. [Run your Cloud Scheduler job](https://docs.cloud.google.com/scheduler/docs/tut-gcf-http.md.txt#run-scheduler-job).
4. [Verify the results in Cloud Run functions](https://docs.cloud.google.com/scheduler/docs/tut-gcf-http.md.txt#verify-results).

## Costs


In this document, you use the following billable components of Google Cloud:


- [Cloud Run](https://cloud.google.com/run/pricing)
- [Cloud Scheduler](https://cloud.google.com/scheduler/pricing)


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

## Before you begin

Note that you must grant the Cloud Run Invoker role because in
[Cloud Run functions](https://docs.cloud.google.com/run/docs/functions/comparison.md.txt),
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:

- [Cloud Run Source Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.sourceDeveloper) (`roles/run.sourceDeveloper`)
- [Cloud Scheduler Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudscheduler.md.txt#cloudscheduler.admin) (`roles/cloudscheduler.admin`)
- [Logs View Accessor](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewAccessor) (`roles/logging.viewAccessor`)
- [Project IAM Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/resourcemanager.md.txt#resourcemanager.projectIamAdmin) (`roles/resourcemanager.projectIamAdmin`)
- [Service Account Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountAdmin) (`roles/iam.serviceAccountAdmin`)
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

> [!NOTE]
> **Note:** [IAM basic
> roles](https://docs.cloud.google.com/iam/docs/roles-overview#basic) might also contain permissions to complete the tutorial. You shouldn't grant basic roles in a production environment, but you can grant them in a development or test environment.

#### Grant the roles

#### Console

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

   [Go to IAM](https://console.cloud.google.com/projectselector/iam-admin/iam?supportedpurview=project)
2. Select the project.
3. Click **Grant access**.
4.
   In the **New principals** field, enter your user identifier. This
   is typically the 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:

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

Replace:

- <var translate="no">PROJECT_NUMBER</var> with your Google Cloud project number.
- <var translate="no">PROJECT_ID</var> with your Google Cloud project ID.
- <var translate="no">PRINCIPAL</var> with the account you are adding the binding for. This is typically the email address that is used to deploy the Cloud Run service.
- <var translate="no">ROLE</var> 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 Run** \>
   **Services** page.


   [Go to Services](https://console.cloud.google.com/run/services)

   <br />

2. Click **Write a function**.

3. In the **Configure** section, do the following:

   1. In the **Service name** field, enter a name for your function.
   2. In the **Region** list, 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](https://docs.cloud.google.com/run/docs/authenticating/public.md.txt) for your
      project, you will need to access your deployed service as described under
      [Test private services](https://docs.cloud.google.com/run/docs/triggering/https-request.md.txt#testing-private).

      For more information, see
      [Authentication overview](https://docs.cloud.google.com/run/docs/authenticating/overview.md.txt).
4. Accept the other defaults and click **Create**.

5. 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](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt).

## 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**
   \> **Jobs** page.


   [Go to Jobs](https://console.cloud.google.com/cloudscheduler)

   <br />

2. 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](https://docs.cloud.google.com/scheduler/docs/configuring/cron-job-schedules.md.txt).
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. To make your HTTP function idempotent and prevent duplicate
processing when Cloud Scheduler retries a job, see
[Deduplicate job requests](https://docs.cloud.google.com/scheduler/docs/configuring/deduplicate-job-requests.md.txt).

## Run your Cloud Scheduler job

You can now run the job you created.

1. In the Google Cloud console, go to the **Cloud Scheduler**
   \> **Jobs** page.


   [Go to Jobs](https://console.cloud.google.com/cloudscheduler)

   <br />

2. Select the checkbox for the job that you created.

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

1. In the Google Cloud console, go to the **Cloud Run**
   \> **Services** page.


   [Go to Services](https://console.cloud.google.com/run/services)

   <br />

2. To filter the list of services for functions, select
   filter_list
   **Filter \> Deployment type \> Function**.

3. Click the function name.

   The **Service details** page opens and displays service metrics.
4. 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

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

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

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
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**
   \> **Jobs** page.


   [Go to Jobs](https://console.cloud.google.com/cloudscheduler)

   <br />

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**
   \> **Services** page.


   [Go to Services](https://console.cloud.google.com/run/services)

   <br />

5. Select the checkbox next to your service.

6. Click **Delete** and confirm your
   deletion.

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


   [Go to Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts)

   <br />

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

9. Click **Delete** and confirm your
   deletion.

## What's next

- [Deduplicate job requests](https://docs.cloud.google.com/scheduler/docs/configuring/deduplicate-job-requests.md.txt)
- [Use Cloud Scheduler to trigger Cloud Run on a schedule](https://docs.cloud.google.com/run/docs/triggering/using-scheduler.md.txt)
- Write [CloudEvents functions](https://docs.cloud.google.com/run/docs/write-functions.md.txt#write-event-driven-functions) to schedule your function to respond to events within your project.