Cloud Run function triggers

After you create and deploy your Cloud Run function, you can set your function to execute in response to various scenarios by specifying a trigger. This page provides an overview of the triggers supported by Cloud Run functions.

Types of triggers

By default, all functions deployed on Cloud Run are Cloud Run source-deployed services that have an HTTP endpoint (run.app URL). If you previously created a function with the gcloud functions command or the Cloud Functions v2 API, by default, the function has a cloudfunctions.net HTTP endpoint.

How your function is invoked or triggered depends on the type of function you specify when writing your code.

  • Event-driven triggers: Reacts to events within your Google Cloud project and corresponds to event-driven functions (also called CloudEvent functions). You must use Eventarc, Google's implementation of the CloudEvents spec that supports building event-driven architectures, to trigger functions in response to specified events.
  • HTTP triggers: Reacts to HTTP(S) requests and correspond to HTTP functions. You can use other Google Cloud services (non-Eventarc) as an event provider to invoke the function through HTTP(S) requests.

To trigger a function to react to events within your Google Cloud project, you specify triggers as part of deploying the function in the Google Cloud console or after function is deployed when using the gcloud CLI.

You cannot bind the same function to more than one trigger at a time, but you can have the same event cause multiple functions to execute by deploying multiple functions with the same trigger settings.

Example triggers

The following are example triggers supported by Cloud Run functions:

All event-driven functions use Eventarc for event delivery. Pub/Sub triggers and Cloud Storage triggers are implemented as particular types of Eventarc triggers.

Additional triggers using Pub/Sub and webhooks

Because Cloud Run functions can be triggered by messages on a Pub/Sub topic, you can integrate Cloud Run functions with any other Google service that supports Pub/Sub as an event bus. In addition, by using HTTP triggers you can also integrate with any service that provides HTTP callbacks (webhooks). The following sections describe some example integrations.

Cloud Logging

Cloud Logging can route logs to a Pub/Sub topic, where they can then be consumed by Cloud Run functions. See Configure and manage sinks in the Cloud Logging documentation for more information.

Cloud Scheduler

You can schedule an HTTP function by creating a Cloud Scheduler job that triggers an action using HTTP.

You can also schedule an event-driven function when your function should be triggered directly in response to events within your Google Cloud project; for example, when a message is published to a Pub/Sub topic.

Cloud Tasks

Cloud Tasks HTTP Target tasks enable you to run task handlers at any HTTP endpoint, so an HTTP function with an HTTP trigger can be used as a task handler. You can also use tasks to throttle or rate-limit function invocations. See Create HTTP Target tasks in the Cloud Tasks documentation for more information.

Gmail

Using the Gmail Push Notification API, you can send Gmail events to a Pub/Sub topic and consume them with Cloud Run.

Trigger permissions

When you create an Eventarc trigger for a Cloud Run function, IAM checks whether you can create the trigger and act as its service account. It doesn't check whether you, or the trigger's service account, can read the event source itself. For example, a principal who can't read a Cloud Storage bucket can still create a trigger that receives an event, including the object metadata, every time an object is created in that bucket.

When you create a trigger, IAM checks the following:

  • You have permission to create the trigger: eventarc.triggers.create, or cloudfunctions.functions.create or cloudfunctions.functions.update if you deploy with the Cloud Functions v2 API, which creates the Eventarc trigger for you.
  • You have iam.serviceAccounts.actAs on the trigger service account.
  • For all event sources except direct Pub/Sub triggers, the trigger service account has the Eventarc Event Receiver role (roles/eventarc.eventReceiver).

IAM doesn't check whether you or the trigger service account have any permissions on the event source resource, such as the database, bucket, or topic that emits the events.

To help limit this risk, do all of the following:

  • Treat trigger-creation permission combined with iam.serviceAccounts.actAs as equivalent to read access to every event source in the Google Cloud project. Grant roles that include eventarc.triggers.create, cloudfunctions.functions.create, or cloudfunctions.functions.update only to principals that you would trust to read that data directly.
  • Grant roles/eventarc.eventReceiver only to dedicated trigger service accounts, not to broadly used accounts such as the default Compute Engine service account, and restrict who has iam.serviceAccounts.actAs on those accounts.
  • Because direct Pub/Sub triggers don't require roles/eventarc.eventReceiver, restrict iam.serviceAccounts.actAs on all service accounts in the Google Cloud project, not only on those with that role.
  • If some principals must be able to deploy functions but must not see data from a particular event source, keep that event source in a separate Google Cloud project.