This tutorial shows you how to trigger an event-driven Cloud Run function using Cloud Scheduler and by targeting a Pub/Sub topic. Use an event-driven function when your function should be triggered directly in response to events within your Google Cloud project, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.
Note that you can also schedule an HTTP function by targeting your function's HTTP endpoint. Use an HTTP function when you need your function to have a URL endpoint and respond to HTTP requests, such as for webhooks. For more information, see Types of Cloud Run functions.
Scheduling the execution of a Cloud Run function is a common use case for Cloud Scheduler. In this tutorial, you will:
- Create a simple Cloud Run 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 Run function was triggered by the Cloud Scheduler job.
Create an event-driven Cloud Run function
Create an event-driven function that is triggered directly in response to events within your Google Cloud project; in this case, a message published to a Pub/Sub topic.
In the Google Cloud console, go to the Cloud Run functions page.
Click
Create function.In the Basics section, do the following:
- In the Environment list, select 2nd gen.
- Enter a name for your function.
- In the Region list, select a region.
In the Trigger section, do the following:
- In the Trigger type list, select Cloud Pub/Sub.
- In the Cloud Pub/Sub topic list, select an existing topic or to create a new topic, click Create a topic. Note the topic's name as you will need it in a subsequent step.
Click More options.
The Eventarc trigger panel opens.
In the Eventarc trigger panel, do the following:
- If you are prompted to allow the Pub/Sub service to create the identity tokens it needs to invoke the function, click Grant.
- In the Service account list, select the service account you previously created.
- Accept the other defaults.
Click Save trigger.
The Eventarc trigger panel closes.
Accept the other defaults and click Next.
Don't modify the default runtime 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
Create a Cloud Scheduler job with a Pub/Sub target.
In the Google Cloud console, go to the Cloud Scheduler page.
Click Click
Create job.Enter a name for your job.
In the Region list, select a region.
Specify the frequency for your job, using a unix-cron format:
30 16 * * 7
For more information, see Cron job format and time zone.
In the Timezone list, select a time zone.
Click Continue.
In the Target type list, select Pub/Sub.
Select the Pub/Sub topic you created earlier.
In the Message body field, enter a string to be sent to your Pub/Sub target topic. For example: "Hello world!"
Click Create.
You have created a cron job that sends a message to your Pub/Sub topic at 16:30 on Sundays. Your Cloud Run function is subscribed to that topic.
Run your Cloud Scheduler job
You can now run the job you created.
In the Google Cloud console, go to the Cloud Scheduler page.
Select the checkbox for the job that you created and click Force run.
When it is initially invoked, the first job created in a project can take a few minutes to configure and run.
After the job runs, the Status of last execution should indicate
Success
.
Verify the results in Cloud Run functions
You can verify that your Cloud Run function is successfully triggered and executed by your cron job.
In the Google Cloud console, go to the Cloud Run functions page.
Click the function name.
The Function details page opens and the Invocations/Second graph shows your first invocation of the function.
Click the Logs tab.
You should see a log entry similar to
Hello, YOUR_STRING!