Schedule a Cloud Function

This tutorial shows you how to trigger a Cloud Function using Cloud Scheduler and Pub/Sub. Being able to schedule the execution of a Cloud Function is a common use case for Cloud Scheduler. You will:

  • Create a simple Cloud Function that subscribes to a Pub/Sub topic.
  • Create a Cloud Scheduler job that publishes a message to the Pub/Sub topic.
  • Run the Cloud Scheduler job.
  • Verify that the Cloud Function was triggered by the Cloud Scheduler job.

Before you begin

This tutorial assumes you have already worked through the Cloud Scheduler Quickstart. As a result, you have the following tools and resources available to you:

  • A Google Cloud project with billing enabled.

  • The Cloud Scheduler API and the Pub/Sub API both enabled on the project. You enable the Cloud Functions API during the tutorial.

Costs

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

  • Cloud Functions
  • Pub/Sub
  • Cloud Scheduler

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.

Create a Cloud Function with a trigger

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

    Go to Cloud Functions

  2. Click Enable API if prompted.

  3. Click Create function. If a message appears prompting to enable APIs, click Enable to activate them.

  4. In the Basics section, select the 2nd gen environment, enter a name for your function, and select a region.

  5. In the Trigger section, under Trigger type, select Cloud Pub/Sub and then click More options. The Eventarc trigger panel opens. If prompted to Enable required APIs, click Enable.

  6. In the Eventarc trigger panel, make sure that the first few fields are filled as follows, or fill them in if necessary:

    • Trigger type: Google sources
    • Event provider: Cloud Pub/Sub
    • Event: google.cloud.pubsub.topic.v1.messagePublished
  7. In the Select a Cloud Pub/Sub topic field, select an existing topic or click Create a topic to create a new one. Your function subscribes to this topic. Make a note of the topic's name - you will need it later.

    • Click Grant if prompted, to allow the Pub/Sub service to create the identity tokens it needs in order to invoke the function.
  8. In the Service account field, click Create new service account.

    1. Enter a name for the service account and click Create. Eventarc uses this service account to invoke the function with an identity token from the Pub/Sub service.

    2. In the Grant this service account access to project section, select the Cloud Run Invoker role. This grants the service account the ability to invoke your 2nd generation function.

    3. Click Done.

  9. Leave Retry on failure unselected. Click Save trigger.

  10. Expand the Runtime, build, connections and security settings section and select the Runtime tab.

  11. Under Runtime service account, in the Service account field, click Create. The service account creation flow opens in a new tab. This service account is the identity that the Cloud Function has when it runs.

    1. Enter a name for the service account.
    2. Click Create and continue.
    3. Click Done.

    You are redirected to the Service accounts page of the Google Cloud console. You can close this tab.

  12. Return to the Service account field on the function creation page, click Refresh, and then select the service account you just created.

  13. Click Next. If prompted to enable any required APIs, click Enable.

  14. Do not modify the default 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

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

    Go to Cloud Scheduler

  2. Click Create job.

  3. Enter a name for your job, select a region for the job to run in, and optionally add a description.

  4. Specify the frequency for your job, using the unix-cron format.

    30 16 * * 7
    

    See Configure cron job schedules for more information.

  5. Select your timezone.

  6. Click Continue.

  7. In the Configure the execution section, select Pub/Sub as the target type.

  8. Select your Pub/Sub topic.

  9. Add a brief Message string to be sent to your target.

  10. Click Create.

You now have 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 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 job that you created.

  3. Click Force run.

    The first job created in a project can take a few minutes to run the first time it is invoked, because of required configuration.

  4. Notice the Status of last execution column. After the job runs, this column should say "Success."

Congratulations! You have just used the console to see the results of running a cron job that sends a message to Pub/Sub. Next, you'll learn how to verify that your Cloud Function was actually executed.

Verify the results in Cloud Functions

To verify that your Cloud Function is being successfully triggered 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.

  3. The Function details page opens. The Invocations/Second graph shows your first invocation of the function. To see details about the invocation, click Logs.

Congratulations! Your function has executed.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial:

Delete the Cloud Scheduler job

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

    Go to Scheduler

  2. Select the checkbox next to your job.

  3. Click Delete and confirm your deletion.

Delete the Pub/Sub topic

  1. In the Google Cloud console, go to the Cloud Pub/Sub page.

    Go to Pub/Sub

  2. Select the checkbox next to your topic.

  3. Click Delete and confirm your deletion.

Delete the Cloud Function

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

    Go to Cloud Functions

  2. Select the checkbox next to your function.

  3. Click Delete and confirm your deletion.

Delete the service accounts

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

    Go to Service Accounts

  2. Select the checkbox next to the service accounts you created.

  3. Click Delete and confirm your deletion.

Disable the APIs

If you enabled any APIs in the course of following this tutorial (such as the Eventarc, Cloud Functions, or Pub/Sub APIs), disable them. Disabling APIs affects access to the API for the project, so do not disable APIs if other resources in your project use them.

  1. In the Google Cloud console, go to the APIs & Services page.

    Go to APIs & Services

  2. Click the API you want to disable. A page describing the API appears.

  3. Click Disable API.

Delete the project

If you created a project just for this tutorial, you can delete it.

  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.