Route Firebase Realtime Database events to Workflows

An Eventarc trigger declares your interest in a certain event or set of events. You can configure event routing by specifying filters for the trigger, including the event source, and the target workflow.

Events are delivered in the CloudEvents format through an HTTP request. The Workflows service converts the event to a JSON object (following the CloudEvents specification) and passes the event into the workflow execution as a workflow runtime argument. Make sure that the event size does not exceed 512 KB. Events larger than the maximum Workflows arguments size won't trigger workflow executions.

These instructions show you how to configure event routing so that an execution of your workflow is triggered in response to a direct Firebase Realtime Database event. For more details, see the list of supported direct events.

Prepare to create a trigger

Before creating an Eventarc trigger for a target workflow, complete the following tasks.

Console

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Enable the Eventarc, Eventarc Publishing, Workflows, and Workflow Executions APIs.

    Enable the APIs

  3. If applicable, enable the API related to the direct events. For example, for Firebase Realtime Database events, enable the Firebase Realtime Database API.

  4. If you don't already have one, create a user-managed service account, then grant it the roles and permissions necessary so that Eventarc can manage events for a target workflow.

    1. In the Google Cloud console, go to the Service Accounts page.

      Go to Service Accounts

    2. Select your project.

    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for event trigger.

    4. Click Create and continue.

    5. To provide appropriate access, in the Select a role list, select the required Identity and Access Management (IAM) roles to grant to your service account. For more information, see Roles and permissions for Workflows targets.

      For additional roles, click Add another role and add each additional role.

    6. Click Continue.

    7. To finish creating the account, click Done.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Enable the Eventarc, Eventarc Publishing, Workflows, and Workflow Executions APIs:

    gcloud services enable eventarc.googleapis.com \
        eventarcpublishing.googleapis.com \
        workflows.googleapis.com \
        workflowexecutions.googleapis.com
    

  3. If applicable, enable the API related to the direct events. For example, for Firebase Realtime Database events, enable firebasedatabase.googleapis.com.

  4. If you don't already have one, create a user-managed service account, then grant it the roles and permissions necessary so that Eventarc can manage events for a target workflow.

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with the name of the service account. It must be between 6 and 30 characters, and can contain lowercase alphanumeric characters and dashes. After you create a service account, you cannot change its name.

    2. Grant the required Identity and Access Management (IAM) roles or permissions. For more information, see Roles and permissions for Workflows targets.

Create a trigger

You can create an Eventarc trigger with a deployed workflow as the event receiver by using the Google Cloud CLI (gcloud or Terraform), or through the Google Cloud console.

Console

  1. In the Google Cloud console, go to the Eventarc Triggers page.

    Go to Triggers

  2. Click Create trigger.
  3. Type a Trigger name.

    This is the ID of the trigger and it must start with a letter. It can contain up to 63 lowercase letters, numbers, or hyphens.

  4. For the Trigger type, select Google sources.
  5. In the Event provider list, select Firebase Realtime Database.

    Note that the event provider name used in the associated Google Cloud documentation might not have a prefix of Cloud or Google Cloud. For example, on the console, Memorystore for Redis is referred to as Google Cloud Memorystore for Redis.

  6. In the Event type list, from the Direct events, select an event type.
  7. In the Event data content type list, select the encoding of the event payload.

    For direct events from Firebase Realtime Database, this must be application/json.

  8. In the Region list, select the same region as the Google Cloud service that is generating events.

    For more information, see Eventarc locations.

  9. In the Attribute 1 field, the instance resource ID acts as an event filter. Select an operator for this filter:
  10. In the Attribute value 1 field, enter the name of the database instance to receive events from. Depending on the operator that you chose in the previous step, the attribute value should be the exact name or a path pattern format.
  11. For the ref attribute, select the Path pattern operator.
  12. In the Attribute value 2 field, enter the database instance path to receive events from if data is created, updated, or deleted in that path or any child path.
  13. Select the Service account that will invoke your service or workflow.

    Or, you can create a new service account.

    This specifies the Identity and Access Management (IAM) service account email associated with the trigger and to which you previously granted specific roles required by Eventarc.

  14. In the Event destination list, select Workflows.
  15. Select a workflow.

    This is the name of the workflow to pass events to. Events for a workflow execution are transformed and passed to the workflow as runtime arguments.

    For more information, see Create a trigger for Workflows.

  16. Click Create.
  17. After a trigger is created, the event source filters cannot be modified. Instead, create a new trigger and delete the old one. For more information, see Manage triggers.

gcloud

gcloud eventarc triggers create TRIGGER \
    --location=LOCATION \
    --destination-workflow=DESTINATION_WORKFLOW  \
    --destination-workflow-location=DESTINATION_WORKFLOW_LOCATION \
    --event-filters="type=EVENT_FILTER_TYPE" \
    --event-filters="instance=INSTANCE" \
    --event-filters-path-pattern="ref=REF" \
    --service-account="MY_SERVICE_ACCOUNT@PROJECT_ID.iam.gserviceaccount.com"

Replace the following:

  • TRIGGER: the ID of the trigger or a fully qualified identifier.

  • LOCATION: the location of the Eventarc trigger. Alternatively, set the eventarc/location property; for example, gcloud config set eventarc/location us-central1. Firebase Realtime Database triggers for Eventarc are only available in the following locations:

    • us-central1
    • europe-west1
    • asia-southeast1

    Note that the trigger must be in the same location as the Firebase Realtime Database instance. For more information, see Realtime Database locations.

  • DESTINATION_WORKFLOW: the ID of the deployed workflow that receives the events from the trigger. The workflow can be in any of the Workflows supported locations and does not need to be in the same location as the trigger. However, the workflow must be in the same project as the trigger.
  • DESTINATION_WORKFLOW_LOCATION (optional): the location in which the destination workflow is deployed. If not specified, it is assumed that the workflow is in the same location as the trigger.
  • EVENT_FILTER_TYPE: the identifier of the Firebase Realtime Database event and can be one of the following:

    • google.firebase.database.ref.v1.created: event is sent when data is created in the database
    • google.firebase.database.ref.v1.updated: event is sent when data is updated in the database
    • google.firebase.database.ref.v1.deleted: event is sent when data is deleted in the database
    • google.firebase.database.ref.v1.written: event is sent when data is created, updated, or deleted in the database
  • INSTANCE: a single database instance and the operator can be one of the following:

    • Equal; for example, --event-filters="instance=INSTANCE"
    • Path pattern; for example, --event-filters-path-pattern="instance=INSTANCE". For more information, see Understand path patterns.
  • REF: the path within the database instance for which you want to receive events from when data is created, updated, or deleted in that path, or any of its children.

  • SERVICE_ACCOUNT_NAME: the name of the IAM service account you created to which you granted specific roles required by Workflows.
  • PROJECT_ID: your Google Cloud project ID

Notes:

  • For direct events from Firebase Realtime Database, the encoding of the event payload is application/json.
  • These flags are required:
    • --event-filters="type=EVENT_FILTER_TYPE"
    • --event-filters="instance=INSTANCE" or --event-filters-path-pattern="instance=INSTANCE"
    • --event-filters-path-pattern="ref=REF"
  • After a trigger is created, the event filter type can't be changed. For a different event type, you must create a new trigger.
  • --service-account: The IAM service account email your Eventarc trigger will use to invoke the workflow executions. We strongly recommend using a service account with the least privileges necessary to access the required resources. To learn more about service accounts, see Create and manage service accounts.
  • By default, Pub/Sub subscriptions created for Eventarc persist regardless of activity and do not expire. To change the inactivity duration, see Subscription properties.

Example:

gcloud eventarc triggers create rd-workflows-trigger \
    --location=us-central1 \
    --destination-workflow=my-workflow \
    --destination-workflow-location=europe-west4 \
    --event-filters="type=google.firebase.database.ref.v1.created" \
    --event-filters="instance=test-instance" \
    --event-filters-path-pattern="ref=users/*" \
    --service-account="${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"

This command creates a trigger called rd-workflows-trigger for the event identified as google.firebase.database.ref.v1.created.

Terraform

You can create a trigger for a workflow using Terraform. For details, see Trigger a workflow using Eventarc and Terraform.

List a trigger

You can confirm the creation of a trigger by listing Eventarc triggers using the Google Cloud CLI or through the Google Cloud console.

Console

  1. In the Google Cloud console, go to the Eventarc Triggers page.

    Go to Triggers

    This page lists your triggers in all locations, and includes details such as names, regions, event providers, destinations, and more.

  2. To filter your triggers:

    1. Click Filter or the Filter triggers field.
    2. In the Properties list, select an option to filter the triggers by.

    You can select a single property or use the logical operator OR to add more properties.

  3. To sort your triggers, beside any supported column heading, click Sort.

gcloud

Run the following command to list your triggers:

gcloud eventarc triggers list --location=-

This command lists your triggers in all locations, and includes details such as names, types, destinations, and statuses.

What's next