Invocation & event triggers
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
- 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.
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.
To integrate a service with Pub/Sub:
- Create a Pub/Sub topic.
- Add code in your service to extract the message from the HTTP request.
- 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.
- Create a service account with the Cloud Run Invoker role to invoke the service.
- Create a Pub/Sub push subscription for the topic, configured with the service account and the service's endpoint URL.
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.