Using Pub/Sub to trigger a Cloud Function

This tutorial shows you how to use Cloud Scheduler and Cloud 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 Cloud Pub/Sub topic.
  • Create a Cloud Pub/Sub topic to trigger that function.
  • Create a Cloud Scheduler job that invokes the Cloud 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 GCP project with billing enabled.

  • An App Engine app located in one of the App Engine supported locations.

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

Costs

This tutorial uses billable components of Cloud Platform, including:

  • Google Cloud Functions
  • Google Cloud Pub/Sub
  • Google Cloud Scheduler

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

New Cloud Platform users might be eligible for a free trial.

Create a Cloud Function:

  1. Visit the Cloud Functions page in the console.

    Go to the Cloud Functions page

  2. Click the Enable API button.

  3. Click the Create function button.

    image

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

  5. Select Cloud Pub/Sub from the Trigger dropdown.

  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. Visit the Cloud Scheduler page in the console.

    Go to the Cloud Scheduler page

  2. Click the Create job button.

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

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

    * * * * *
    

    See Configuring Cron Job Schedules for more information.

  5. Select your timezone.

  6. Select Pub/Sub from the Target dropdown.

  7. Enter the Topic name you selected above.

  8. Add a brief Payload string to be sent to your target.

  9. Click Create.

You now have a cron job that sends a message to your Cloud Pub/Sub topic every minute. Your Cloud Function is subscribed to that topic.

Let's run the job you just created.

Run your job

  1. Open the Cloud Scheduler console page.

    Go to the Cloud Schedulerpage

  2. Click the Run now button.

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

  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 Cloud 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. Visit the Cloud Functions page in the console.

    Go to the Cloud Functions page

  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 GCP account for the resources used in this tutorial:

Delete the Cloud Scheduler job

  1. Go to the Cloud Scheduler page in the GCP Console.

    Go to the Scheduler page

  2. Click the checkbox next to your job.

  3. Click the Delete button at the top of the page and confirm your delete.

Delete the Pub/Sub topic

  1. Go to the Cloud Pub/Sub page in the GCP Console.

    Go to the Pub/Sub page

  2. Click the checkbox next to your topic.

  3. Click Delete at the top of the page and confirm your delete.

Delete the Cloud Functions function

  1. Go to the Cloud Functions page in the GCP Console.

    Go to the Cloud Functions page

  2. Click the checkbox next to your function.

  3. Click the Delete button at the top of the page and confirm your delete.

Delete the project

If you created a project just for this tutorial.

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete .
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Was deze pagina nuttig? Laat ons weten hoe goed we u hebben geholpen:

Feedback verzenden over...

Cloud Scheduler Documentation