Create triggers with Eventarc

This page shows you how to create an Eventarc trigger so that a Cloud Run service can receive events from another Google Cloud service.

Eventarc is a Google Cloud service that lets you build event-driven architectures without having to implement, customize, or maintain the underlying infrastructure.

You can create an Eventarc trigger by specifying filters for the trigger and configuring the routing of the event, including the event source and the target Cloud Run service. When the specified event or set of events match the filters, your Cloud Run service is automatically invoked in response to the events. A service that uses Eventarc triggers is called an event-driven service. Events sent to your Cloud Run service are received in the form of HTTP requests.

The following event types can trigger requests to your service:

Trigger location

When you create an Eventarc trigger, you specify a location for it. This should match the location of the Google Cloud resource that you want to monitor for events. In most scenarios, you should also deploy your event-driven Cloud Run service in the same region. For more information, see Understand Eventarc locations.

Trigger identity

When you create an Eventarc trigger, you specify an Identity and Access Management (IAM) service account for it. The trigger uses this service account as its default identity. We recommend that you specify a user-managed service account and grant it the minimum permissions required.

Before you begin

  1. Set up your Cloud Run environment.

  2. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Eventarc APIs:

    Enable the APIs

  3. Grant the required IAM roles and permissions.

Create a trigger for services

After deploying a service, you can configure a trigger using the Google Cloud console, Google Cloud CLI, or Terraform.

Console

  1. Deploy your Cloud Run service using containers or from source.

  2. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  3. From the list of services, click an existing service.

  4. On the Service details page, navigate to the Triggers tab.

  5. Click Add trigger, and select an option.

  6. In the Eventarc trigger pane, modify the trigger details as follows:

    1. In the Trigger name field, enter a name for the trigger, or use the default name.

    2. Select a Trigger type from the list to specify one of the following trigger types:

      • Google Sources to specify triggers for Pub/Sub, Cloud Storage, Firestore, and other Google event providers.

      • Third-party to integrate with non-Google providers that offer an Eventarc source. For more information, see Third-party events in Eventarc.

    3. Select an event provider from the Event provider list, to select a product that provides the type of event for triggering your service. For the list of event providers, see Event providers and destinations.

    4. Select an event type from the Event type list. Your trigger configuration varies depending on the supported event type. For more information, see Event types.

    5. If the Region field is enabled, select a location for the Eventarc trigger. In general, the location of an Eventarc trigger should match the location of the Google Cloud resource that you want to monitor for events. In most scenarios, you should also deploy your service in the same region. See Understand Eventarc locations for more details about Eventarc trigger locations.

    6. In the Service account field, select a service account. Eventarc triggers are linked to service accounts to use as an identity when invoking your service. Your Eventarc trigger's service account must have the permission to invoke your service. By default, Cloud Run uses the Compute Engine default service account.

    7. Optionally, specify the Service URL path to send the incoming request to. This is the relative path on the destination service to which the events for the trigger should be sent. For example: /, /route, route, and route/subroute.

    8. Optionally, to enable retries if the delivery attempt fails, select the Enable retry on failure checkbox; otherwise, the default behavior is a single delivery attempt with no retries. For more information, see Retry events.

    9. Once you've completed the required fields, click Save trigger.

  7. After creating the trigger, verify its health by ensuring that there is a checkmark on the Triggers tab.

gcloud

  1. Deploy your Cloud Run service using containers or from source.

  2. Run the following command to create a trigger that filters and routes events:

    gcloud eventarc triggers create TRIGGER_NAME  \
        --location=LOCATION \
        --destination-run-service=DESTINATION_RUN_SERVICE  \
        --destination-run-region=DESTINATION_RUN_REGION \
        --event-filters="type=EVENT_FILTER_TYPE" \
        --max-retry-attempts=1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
    

    Replace the following:

    • TRIGGER_NAME: the ID of the trigger or a fully qualified identifier.
    • LOCATION: the location of the Eventarc trigger. Alternatively, you can set the eventarc/location property; for example, gcloud config set eventarc/location us-central1.

      To avoid any performance and data residency issues, the location must match the location of the Google Cloud service that is generating events. For more information, see Eventarc locations.

    • DESTINATION_RUN_SERVICE: the name of the Cloud Run service that receives the events for the trigger. The service can be in any of the Cloud Run supported locations and doesn't need to be in the same location as the trigger. However, the service must be in the same project as the trigger and will receive events as HTTP POST requests sent to its root URL path (/), whenever the event is generated.
    • DESTINATION_RUN_REGION: (optional) the Cloud Run location in which the destination Cloud Run service can be found. If not specified, it is assumed that the service is in the same region as the trigger.
    • EVENT_FILTER_TYPE: the identifier of the event. An event is generated when an API call for the method succeeds. For long-running operations, the event is only generated at the end of the operation, and only if the action is performed successfully. For a list of supported event types, see Google event types supported by Eventarc.
    • SERVICE_ACCOUNT_NAME: the name of your user-managed service account.
    • PROJECT_ID: your Google Cloud project ID.

    Notes:

    • The --event-filters="type=EVENT_FILTER_TYPE" flag is required. If no other event filter is set, events for all resources are matched.
    • EVENT_FILTER_TYPE can't be changed after creation. To change EVENT_FILTER_TYPE, create a new trigger and delete the old one.
    • Each trigger can have multiple event filters, comma delimited in one --event-filters=[ATTRIBUTE=VALUE,...] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported; however, when using the --event-filters-path-pattern flag, you can define a resource path pattern.
    • Optionally, to specify a single event delivery attempt with no retries, use the --max-retry-attempts flag. The only valid value is 1. If you omit the flag, the standard retry behavior applies. For more information, see Retry events.
    • Other flags are available. For more information, see gcloud eventarc triggers create.

Terraform

To create an Eventarc trigger for a Cloud Run service, see Create a trigger using Terraform.

Create a trigger for functions

After deploying a function, you can configure a trigger using the Google Cloud console, Google Cloud CLI, or Terraform.

Console

When you use the Google Cloud console to create a function, you can also add a trigger to your function. Follow these steps to create a trigger for your function:

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click Write a function, and enter the function details. For more information about configuring functions during deployment, see Deploy functions.

  3. In the Trigger section, click Add trigger.

  4. Select an option.

  5. In the Eventarc trigger pane, modify the trigger details as follows:

    1. Enter a name for the trigger in the Trigger name field, or use the default name.

    2. Select a Trigger type from the list:

      • Google Sources to specify triggers for Pub/Sub, Cloud Storage, Firestore, and other Google event providers.

      • Third-party to integrate with non-Google providers that offer an Eventarc source. For more information, see Third-party events in Eventarc.

    3. Select an event provider from the Event provider list, to select a product that provides the type of event for triggering your function. For the list of event providers, see Event providers and destinations.

    4. Select an event type from the Event type list. Your trigger configuration varies depending on the supported event type. For more information, see Event types.

    5. If the Region field is enabled, select a location for the Eventarc trigger. In general, the location of an Eventarc trigger should match the location of the Google Cloud resource that you want to monitor for events. In most scenarios, you should also deploy your function in the same region. See Understand Eventarc locations for more details about Eventarc trigger locations.

    6. In the Service account field, select a service account. Eventarc triggers are linked to service accounts to use as an identity when invoking your function. Your Eventarc trigger's service account must have the permission to invoke your function. By default, Cloud Run uses the Compute Engine default service account.

    7. Optionally, specify the Service URL path to send the incoming request to. This is the relative path on the destination service to which the events for the trigger should be sent. For example: /, /route, route, and route/subroute.

    8. Optionally, to enable retries if the delivery attempt fails, select the Enable retry on failure checkbox; otherwise, the default behavior is a single delivery attempt with no retries. For more information, see Retry events.

  6. Once you've completed the required fields, click Save trigger.

  7. Click Create.

  8. In the Source tab, edit the source code if needed, then select Save and redeploy.

gcloud

When you create a function using the gcloud CLI, you must first deploy your function, and then create a trigger. Follow these steps to create a trigger for your function:

  1. Run the following command in the directory that contains the sample code to deploy your function:

    gcloud run deploy FUNCTION \
        --source . \
        --function FUNCTION_ENTRYPOINT \
        --base-image BASE_IMAGE_ID \
        --region REGION
    

    Replace the following:

    • FUNCTION: the name of the function you are deploying. You can omit this parameter entirely, but you will be prompted for the name if you omit it.

    • FUNCTION_ENTRYPOINT: the entry point to your function in your source code. This is the code Cloud Run executes when your function runs. The value of this flag must be a function name or fully-qualified class name that exists in your source code.

    • BASE_IMAGE_ID: the base image environment for your function. For more details about base images and the packages included in each image, see Runtimes base images.

    • REGION: the Google Cloud region where you want to deploy your function. For example, europe-west1.

  2. Run the following command to create a trigger that filters and routes events:

    gcloud eventarc triggers create TRIGGER_NAME \
        --location=LOCATION \
        --destination-run-service=FUNCTION \
        --destination-run-region=DESTINATION_RUN_REGION \
        --event-filters="type=EVENT_FILTER_TYPE" \
        --max-retry-attempts=1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
    

    Replace the following:

    • TRIGGER_NAME: the ID of the trigger or a fully qualified identifier.
    • LOCATION: the location of the Eventarc trigger. Alternatively, you can set the eventarc/location property; for example, gcloud config set eventarc/location us-central1.

      To avoid any performance and data residency issues, the location must match the location of the Google Cloud service that is generating events. For more information, see Eventarc locations.

    • FUNCTION: the name of the deployed Cloud Run function that receives the events for the trigger.
    • DESTINATION_RUN_REGION: (optional) the Cloud Run location in which the destination Cloud Run function can be found. If not specified, it is assumed that the function is in the same region as the trigger.
    • EVENT_FILTER_TYPE: the identifier of the event. An event is generated when an API call for the method succeeds. For long-running operations, the event is only generated at the end of the operation, and only if the action is performed successfully. For a list of supported event types, see Google event types supported by Eventarc.
    • SERVICE_ACCOUNT_NAME: the name of your user-managed service account.
    • PROJECT_ID: your Google Cloud project ID.

    Notes:

    • The --event-filters="type=EVENT_FILTER_TYPE" flag is required. If no other event filter is set, events for all resources are matched.
    • EVENT_FILTER_TYPE can't be changed after creation. To change EVENT_FILTER_TYPE, create a new trigger and delete the old one.
    • Each trigger can have multiple event filters, comma delimited in one --event-filters=[ATTRIBUTE=VALUE,...] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported; however, when using the --event-filters-path-pattern flag, you can define a resource path pattern.
    • Optionally, to specify a single event delivery attempt with no retries, use the --max-retry-attempts flag. The only valid value is 1. If you omit the flag, the standard retry behavior applies. For more information, see Retry events.
    • Other flags are available. For more information, see gcloud eventarc triggers create.

Terraform

To create an Eventarc trigger for a Cloud Run function, see Create a trigger using Terraform.

Set the Pub/Sub acknowledgement deadline

Event-driven Cloud Run functions are implemented using Eventarc in combination with a Pub/Sub subscription. By default, the acknowledgement (ack) deadline of this Pub/Sub subscription is 10 seconds. This setting is insufficient for many functions, and can have the effect of causing unwanted duplicate executions.

We recommend that you set the ack deadline for your service or function to the maximum value of 600 seconds as follows:

Console

After you deploy your function, follow these steps to modify the function's ack deadline:

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Locate the function you want to update in the Services list, and click to open the details of that function.

  3. Open the Triggers tab.

  4. Click the trigger name to open Trigger details.

  5. Click the Topic link to open the topic editing pane.

  6. Click the Subscription ID name to go to the subscription pane, and click Edit at the top of the page.

  7. Set the value for the Acknowledgement Deadline to 600 and click Update to save the change.

gcloud

Update the ack deadline per trigger to the maximum value of 600 seconds. Note that the following commands reference variables (TRIGGER_NAME and LOCATION) whose values you set in the previous steps.

## Per Cloud Run function:

# Update Ack Deadline to max (600s)
SUBSCRIPTION_ID=$(gcloud eventarc triggers describe "$TRIGGER_NAME" --location $LOCATION --format json | jq -r '.transport.pubsub.subscription')
gcloud pubsub subscriptions update "$SUBSCRIPTION_ID" --ack-deadline=600

Perform a bulk update on all service and function triggers to set their ack deadlines to 600 seconds:

### Match all Cloud Run service triggers and update all deadlines to 600s (max timeout)
### This will change ALL Cloud Run services and Cloud Run functions
TRIGGER_NAMES=($(gcloud eventarc triggers list | awk '/Cloud Run service/ {print $1}'))

if [ ${#TRIGGER_NAMES[@]} -eq 0 ]; then
  echo "No matching triggers found"
fi

for trigger in "${TRIGGER_NAMES[@]}"; do
echo "Updating ack deadline for trigger: $trigger"
SUBSCRIPTION_ID=$(gcloud eventarc triggers describe "$trigger" --location $LOCATION --format json | jq -r '.transport.pubsub.subscription')

if [ -z "$SUBSCRIPTION_ID" ]; then
    echo "Error: Could not retrieve subscription ID for trigger: $trigger"
    continue # Skip to the next trigger
fi
gcloud pubsub subscriptions update "$SUBSCRIPTION_ID" --ack-deadline=600
echo "Ack deadline updated for subscription: $SUBSCRIPTION_ID"
done

View the CloudEvent ID and source

To view the CloudEvent ID and source that triggered your service, view the following resources in Cloud Run service logs:

  • LogEntry.labels.run.googleapis.com/cloud_event_id
  • LogEntry.labels.run.googleapis.com/cloud_event_source

What's next