Schedule an event-driven Cloud Function


This tutorial shows you how to trigger an event-driven Cloud Function using Cloud Scheduler and by targeting a Pub/Sub topic. Use 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.

Note that you can also schedule an HTTP function by targeting your function's 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. For more information, see Types of Cloud Functions.

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

  1. Create a simple Cloud Function that subscribes to a Pub/Sub topic.
  2. Create a Cloud Scheduler job that publishes a message to the Pub/Sub topic.
  3. Run the Cloud Scheduler job.
  4. Verify that the Cloud 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, Eventarc 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, Eventarc 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 Functions (2nd gen), invocation permissions are available by managing the underlying Cloud Run service.

  11. By default, Cloud 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 event-driven Cloud Function

Create an event-driven function that is triggered directly in response to events within your Google Cloud project; in this case, a message published to a Pub/Sub topic.

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

    Go to Cloud Functions

  2. Click Create function.

  3. In the Basics section, do the following:

    1. In the Environment list, select 2nd gen.
    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 Cloud Pub/Sub.
    2. In the Cloud Pub/Sub topic list, select an existing topic or to create a new topic, click Create a topic. Note the topic's name as you will need it in a subsequent step.
    3. Click More options.

      The Eventarc trigger panel opens.

  5. In the Eventarc trigger panel, do the following:

    1. If you are prompted to allow the Pub/Sub service to create the identity tokens it needs to invoke the function, click Grant.
    2. In the Service account list, select the service account you previously created.
    3. Accept the other defaults.
    4. Click Save trigger.

      The Eventarc trigger panel closes.

  6. Accept the other defaults and click Next.

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

Create a Cloud Scheduler job

Create a Cloud Scheduler job with a Pub/Sub target.

  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 Pub/Sub.

  9. Select the Pub/Sub topic you created earlier.

  10. In the Message body field, enter a string to be sent to your Pub/Sub target topic. For example: "Hello world!"

  11. Click Create.

You have created a cron job that sends a message to your Pub/Sub topic at 16:30 on Sundays. Your Cloud Function is subscribed to that topic.

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 Functions

You can verify that your Cloud Function is successfully triggered and executed by your cron job.

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

    Go to Cloud 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 Hello, YOUR_STRING!

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 Pub/Sub page.

    Go to Pub/Sub

  5. Select the checkbox next to your topic.

  6. Click Delete and confirm your deletion.

  7. In the Google Cloud console, go to the Cloud Functions page.

    Go to Cloud Functions

  8. Select the checkbox next to your function.

  9. Click Delete and confirm your deletion.

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

    Go to Service Accounts

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

  12. Click Delete and confirm your deletion.

What's next