Using Pub/Sub to trigger a Cloud Function

Stay organized with collections Save and categorize content based on your preferences.

This tutorial shows you how to use Cloud Scheduler and Pub/Sub to trigger a Cloud Function. 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 Pub/Sub topic to trigger that function.
  • Create a Cloud Scheduler job that invokes the Pub/Sub trigger.
  • Run the Cloud Scheduler job.
  • Verify success.

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

This tutorial uses 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:

  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. Select the 2nd gen environment, enter a name for your function, and select a region.

  5. In the Trigger section, select Require authentication and click Add Eventarc trigger. Click Enable Eventarc API if prompted.

  6. For the Eventarc trigger, select First-party trigger type, Pub/Sub as the Event provider, and google.cloud.pubsub.topic.v1.messagePublished as the Event.

    Select an existing topic from the dropdown, or use the Create new topic pop-up to create a new one. Your function is now subscribed to the 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 will need to use to trigger the function. Recall that you selected Require authentication earlier for the function earlier. Pub/Sub uses the identity tokens for that required authentication.

    Under Service account select Create new service account.

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

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

    • Click Done.

    Leave Retry on failure unselected. Click Save trigger.

  7. Click Runtime, build, connections and security settings and select the Runtime tab. For Runtime service account select Create new service account. This is the identity that the Cloud Function has when it runs. Enter a name for the service account and click Create Service Account.

    The function for this tutorial does not require access to project resources, so this new account does not require any roles granted to it. If your function code needs access to other resources, allow them for this service account using IAM.

    image

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

  9. 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 a job.

  3. If necessary, select a region for your job to run in.

  4. Give your job a name and optionally add a description.

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

    30 16 * * 7
    

    See Configure cron job schedules for more information.

  6. Select your timezone.

  7. Click Continue

  8. In the Target list, select Pub/Sub.

  9. Select your Topic name.

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

  11. 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. Click Run now.

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

  3. Notice the Result column.

    image

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.

    image

  3. The Function details page opens. Check invocations.

    image

    Congratulations! Your function has executed.

  4. Click View Logs.

    image

  5. See the details about the invocation.

    image

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

Delete the project

If you created a project just for this tutorial.

  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.