Skip to main content

Triggering Cloud Run functions

Exam guide§2.1

You set a Cloud Run function to run in response to something by giving it a trigger when you deploy it. Triggers come in two categories: an HTTP trigger (reacts to HTTP(S) requests, backs an HTTP function) and an event trigger (reacts to an event in your Google Cloud project, backs an event-driven function). This page covers how triggers are specified, which sources are supported, and the shape of each trigger type.

Specifying triggers

One trigger can fan out to many functions (deploy several functions with the same trigger source), but a single function cannot be bound to more than one trigger at a time.
  • Triggers are specified as part of function deployment.
  • The same event can invoke multiple functions - deploy several functions with the same trigger source settings.
  • For event-driven functions, you create Eventarc event triggers using filters. A filter can match the service name, method name, event type, and other information. Create them in the Google Cloud console or with the gcloud CLI.
GotchaOne function, one trigger

A function cannot be bound to more than one trigger at a time. Fan-out is fine (one trigger source can drive many functions), but the reverse is not - a single function has exactly one trigger.

Supported triggers

All event-driven Cloud Run functions use Eventarc for event delivery. Eventarc supports 90+ Google Cloud sources, including events from Cloud Audit Logs, external SaaS event sources, and custom sources (by publishing to Pub/Sub).

Cloud Run functions(1st gen)Cloud Run functions(2nd gen)TriggercategoryHTTPHTTPPub/SubPub/SubCloud StorageCloud StorageFirestoreFirestoreFirebaseEventarc90+ event sources
Both generations trigger from HTTP, Pub/Sub, Cloud Storage, and Firestore. Where 1st gen exposes Firebase as its own source, Cloud Run functions generalizes to Eventarc, delivering from 90+ event sources.

Because a function can be triggered by messages on a Pub/Sub topic, you can integrate any Google service that uses Pub/Sub as an event bus:

FactsAdditional triggers via Pub/Sub
  • Cloud Logging and Cloud Scheduler - route their events through Pub/Sub to a function.
  • Cloud Tasks - an HTTP Cloud Run function can act as a task handler.
  • Gmail - the Gmail Push Notification API sends Gmail events to a Pub/Sub topic, consumed by a function.

HTTP trigger

An HTTP trigger enables a function to run in response to HTTP(S) requests. When assigned, the function gets an HTTPS endpoint (URL) at which it can be invoked.

FactsSupported HTTP request methods
  • GET, POST, PUT, DELETE, OPTIONS.

Pub/Sub trigger

Pub/SubTopicEvent dataEvent-drivenfunction
A Pub/Sub trigger names a topic; every message published to that topic delivers event data to the event-driven function.
  • A Pub/Sub topic must be specified. The function is called whenever a message is published to that topic.
  • The function must be an event-driven function.
  • In Cloud Run functions, a Pub/Sub trigger is implemented as a type of Eventarc trigger.
  • Event data is passed in CloudEvents format (for a CloudEvent function) or PubsubMessage format (for a Background function).

Cloud Storage trigger

Cloud StorageEvent typeEvent dataEvent-drivenfunction
A Cloud Storage trigger names an event type and a bucket; a matching change on an object delivers event data to the event-driven function.
  • You choose an event type and a Cloud Storage bucket. The function is called whenever a change of that type occurs on an object (file) in the bucket.
  • The function must be an event-driven function.
  • Event data is passed in CloudEvents format or StorageObjectData format.
  • Implemented as a type of Eventarc trigger.
FactsCloud Storage event types
  • Object finalized - a new object is created (or an overwrite completes).
  • Object deleted.
  • Object archived.
  • Object metadata updated.

For the IAM plumbing a Cloud Storage trigger needs before deploy, see Cloud Storage triggers need IAM plumbing first.

Firestore trigger

FirestoreEvent typeDocumentEvent dataEvent-drivenfunction
A Firestore trigger names an event type and a document path; a matching event on a document delivers a snapshot of that document to the event-driven function.
  • You choose an event type and a document path. The function is invoked when an event of that type occurs on a document, and it receives a snapshot of the affected document.
  • Firestore supports create, update, delete, and write events (onCreate, onUpdate, onDelete, onWrite, where write fires on any of the other three).
  • The document path can reference a specific document (users/john) or a wildcard pattern (users/{userId}), and must not contain a trailing slash.
  • Firestore must be in the same Google Cloud project as the function.
  • The function can be a CloudEvent or Background function depending on the language runtime.
GotchaNative mode only

Firestore triggers are available only for Firestore in Native mode. They are not available for Firestore in Datastore mode.

GotchaDocument level only

A Firestore trigger applies only at the document level. You cannot create a trigger for a specific document field or for a collection.

Firebase triggers

FirebaseEvent typeEvent dataEvent-drivenfunction
A Firebase trigger handles events from a Firebase service in the same project; each service exposes its own event types and config resources.

Cloud Run functions supports triggers for several Firebase services. Each uses its own event types and config resources, and the Firebase service must be in the same Google Cloud project as the function.

NumbersSupported Firebase services
  • Google Analytics for Firebase - 1st generation only.
  • Firebase Realtime Database.
  • Firebase Authentication - 1st generation only.
  • Firebase Remote Config.
GotchaTwo Firebase sources are 1st-gen only

Google Analytics for Firebase and Firebase Authentication triggers are supported only on 1st-generation Cloud Run functions - they are not available on 2nd-gen functions.