Eventarc lets you trigger Cloud Functions from any Google Cloud event, not just Pub/Sub or Storage.

Let’s see it in action. Imagine we want to trigger a Cloud Function whenever a new row is added to a BigQuery table.

First, we need a BigQuery dataset and table.

-- Create a dataset
CREATE SCHEMA my_dataset;

-- Create a table
CREATE TABLE my_dataset.my_table (
    id INT64,
    name STRING
);

Now, we’ll create a simple Cloud Function that logs the data from the BigQuery row.

# main.py
import base64
import json

def bq_trigger(event, context):
    """Triggered by a change to a BigQuery table.
    Args:
         event (dict): Event payload.
         context (object): Metadata for the event.
    """
    print(f"Event ID: {context.event_id}")
    print(f"Event Type: {context.event_type}")
    print(f"Timestamp: {context.timestamp}")

    # The structure of the 'event' payload depends on the event source.
    # For BigQuery, it's a Data Change Event.
    # See: https://cloud.google.com/bigquery/docs/change-data-capture#data_change_events
    if 'data' in event:
        for row in event['data']:
            print(f"Row ID: {row['after']['id']}, Name: {row['after']['name']}")
    else:
        print("No data found in event.")

Deploy this function with the --trigger-event-filters flag to specify the BigQuery event source and table.

gcloud functions deploy bq_trigger_function \
  --runtime python39 \
  --trigger-event-filters="type=google.cloud.bigquery.dataset.v1.row.inserted" \
  --trigger-event-filters="table_name=projects/YOUR_PROJECT_ID/datasets/my_dataset/tables/my_table" \
  --entry-point bq_trigger \
  --region us-central1

Now, when you insert a row into my_dataset.my_table:

INSERT INTO my_dataset.my_table (id, name) VALUES (1, 'Alice');

Your bq_trigger_function will be invoked, and you’ll see output similar to this in your Cloud Functions logs:

Event ID: 1234567890123456
Event Type: google.cloud.bigquery.dataset.v1.row.inserted
Timestamp: 2023-10-27T10:00:00.123Z
Row ID: 1, Name: Alice

Eventarc acts as the glue. It subscribes to the change stream of your BigQuery table (or any other supported GCP service) via Cloud Audit Logs or dedicated event channels. When a matching event occurs, Eventarc publishes it to a Google-managed Pub/Sub topic. Your Cloud Function, configured with a Pub/Sub trigger, then consumes this event.

The key to Eventarc is its declarative approach to event filtering. You specify what you want to listen for (type) and where it should come from (table_name, bucket, topic, etc.) using the --trigger-event-filters flag. Eventarc handles the underlying infrastructure, ensuring that events are reliably delivered to your function.

This pattern decouples your services. The BigQuery service doesn’t need to know about your Cloud Function. It simply emits events. Eventarc listens and routes them. Your Cloud Function only cares about reacting to the event data it receives.

The type filter is crucial. For BigQuery, common types include google.cloud.bigquery.dataset.v1.row.inserted, google.cloud.bigquery.dataset.v1.row.updated, and google.cloud.bigquery.dataset.v1.row.deleted. You can find the full list of event types for each service in the Eventarc documentation.

The table_name filter, in this BigQuery example, uses a fully qualified resource name. For Cloud Storage, you’d use a bucket filter like bucket=your-bucket-name. For Pub/Sub, you’d typically filter by topic like topic=your-topic-name.

What most people miss is that Eventarc doesn’t just offer a few pre-canned triggers. It leverages the comprehensive eventing capabilities of Google Cloud, including Cloud Audit Logs for many services. This means that if an action is logged in Cloud Audit Logs, there’s a high probability Eventarc can trigger off of it, opening up a vast array of possibilities beyond the most common event sources. You can filter on specific fields within the audit log entry, giving you granular control.

The next step is to explore how to handle multiple event types for a single function or how to use Eventarc to trigger other services besides Cloud Functions, like Workflows or even custom applications via Pub/Sub subscriptions.

Want structured learning?

Take the full Cloud-functions course →