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.
An App Engine app located in one of the App Engine supported locations.
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 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:
Visit the Cloud Functions page in the console.
Click the Enable API button.
Click the Create function button.
Give your function a name. Accept the default memory allocation.
Select Cloud Pub/Sub from the Trigger dropdown.
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.
Accept the defaults for everything else, including the sample
index.js
andpackage.json
code.Click Create.
Create a Cloud Scheduler job
Visit the Cloud Scheduler page in the console.
Click the Create job button.
If necessary, select a region for your job to run in.
Give your job a name and optionally add a description.
Specify the frequency for your job, using the unix-cron format.
* * * * *
See Configuring Cron Job Schedules for more information.
Select your timezone.
Select Pub/Sub from the Target dropdown.
Enter the Topic name you selected above.
Add a brief Payload string to be sent to your target.
Click Create.
You now have a cron job that sends a message to your Pub/Sub topic every minute. Your Cloud Function is subscribed to that topic.
Let's run the job you just created.
Run your job
Open the Cloud Scheduler console page.
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.
Notice the Result column.
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:
Visit the Cloud Functions page in the console.
Click the function name.
The Function details page opens. Check invocations.
Congratulations! Your function has executed.
Click View Logs.
See the details about the invocation.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial:
Delete the Cloud Scheduler job
Go to the Cloud Scheduler page in the Cloud Console.
Click the checkbox next to your job.
Click the Delete button at the top of the page and confirm your delete.
Delete the Pub/Sub topic
Go to the Cloud Pub/Sub page in the Cloud Console.
Click the checkbox next to your topic.
Click Delete at the top of the page and confirm your delete.
Delete the Cloud Functions function
Go to the Cloud Functions page in the Cloud Console.
Click the checkbox next to your function.
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.
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.