Skip to main content

Invocation & event triggers

Exam guide§2.1

A Cloud Run service does nothing until something invokes it. That "something" is either a direct call (a client, webhook, or gRPC request) or an event trigger that fans a Google Cloud event into the service. This page covers both. (For deploying a single function instead of a whole container, see Cloud Run functions.)

Event triggers

How does it work?INVOKE / TRIGGERSPre-packaged web app3rd party servicesPrivate microservicesMiddlewareConnect internalmicroservicesHigh loads of data(Faster than REST)Other Google CloudservicesBackupRecurrent admin tasks (sync data)Generating docs (bills)Events from Google CloudSources and 3rd party servicesEvents in CloudEvents formathttps://WebhookgRPCPub/SubCloud SchedulerEventarcEventsFirebaseMobile AppsCloud Run
Everything reaches Cloud Run through one of two paths: request-style invocations (a Webhook over https:// or a gRPC call, and Firebase for mobile apps) flow straight in, while event triggers (Pub/Sub, Cloud Scheduler publishing to a topic, and Eventarc routing CloudEvents) fan in from Google Cloud sources and scheduled jobs.
  • HTTP - direct invocation.
  • Pub/Sub - message on a topic invokes the function/service (see below).
  • Cloud Storage events - object finalize/delete/archive/metadata-update. For a function triggered by a bucket, see Cloud Run functions.
  • Eventarc - unified event routing from 90+ Google sources (and Audit Logs) to Cloud Run. Preferred way to wire GCP events to Cloud Run services.
  • Cloud Scheduler - securely invoke a service on a schedule (managed cron), e.g. regular backups, sitemap regeneration, generating bills.
  • Cloud Tasks - securely enqueue work to be processed asynchronously by a service.
GotchaStateless only

Cloud Run/functions instances are ephemeral - no local persistent state, no guaranteed instance affinity. Store state in Cloud SQL, Firestore, Memorystore, or GCS. Long background work after the response ends may be killed unless CPU is always allocated.

Triggering from Pub/Sub

Pub/Sub can push messages to your Cloud Run service's endpoint, where they arrive as HTTP requests. The endpoint can be protected with IAM and does not need to be public.

Client applicationPub/SubPub/Sub TopicPush subscriptionCloud RunService RevisionContainerPublish messageHTTPS endpoint:https:// *.run.apphttps://your.domain
A client publishes a message to a Pub/Sub topic, whose push subscription delivers it over HTTPS to the container running inside the Cloud Run service revision.

To integrate a service with Pub/Sub:

NumbersSteps to integrate with Pub/Sub
  1. Create a Pub/Sub topic.
  2. Add code in your service to extract the message from the HTTP request.
  3. Respond with an appropriate HTTP status code:
    • Success (e.g. 200, 204) acknowledges complete processing.
    • Error (e.g. 400, 500) tells Pub/Sub to redeliver the message.
  4. Create a service account with the Cloud Run Invoker role to invoke the service.
  5. Create a Pub/Sub push subscription for the topic, configured with the service account and the service's endpoint URL.
GotchaAcknowledge within 600 seconds

Your service must return a response within the 600-second maximum acknowledgement deadline. Otherwise Pub/Sub redelivers the message and the service is triggered again - handlers must be idempotent.

Recap

DECISIONHow should something invoke my service?
A client or webhook calls it directlyHTTP / gRPC invocation
React to a message on a topicPub/Sub push subscription
React to a Cloud Storage object changeEventarc (or a Cloud Storage trigger)
React to many GCP event sources uniformlyEventarc
Run on a schedule (managed cron)Cloud Scheduler
Enqueue async workCloud Tasks
Pick this when: match the source to the trigger
0%0 of 147 pages studied