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.

  3. Click Create function.

    image

  4. Give your function a name. Accept the default memory allocation.

  5. In the Trigger list, select Cloud Pub/Sub.

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

  7. Accept the defaults for everything else, including the sample index.js and package.json code.

  8. Click Create.

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