0
0
GCPcloud~5 mins

Eventarc for event routing in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Eventarc helps you send events from one place to another in Google Cloud. It makes sure your apps get the right messages when something important happens.
When you want to trigger a cloud function after a file is uploaded to Cloud Storage.
When you need to send notifications to different services after a database change.
When you want to connect events from multiple Google Cloud services to your app.
When you want to route events from custom sources to Cloud Run services.
When you want to build apps that react automatically to changes in your cloud environment.
Config File - eventarc-trigger.yaml
eventarc-trigger.yaml
apiVersion: eventarc.googleapis.com/v1
kind: Trigger
metadata:
  name: example-trigger
  namespace: default
spec:
  serviceAccount: example-service-account@my-project.iam.gserviceaccount.com
  destination:
    cloudRunService:
      service: example-service
      region: us-central1
  matchingCriteria:
  - attribute: type
    value: google.cloud.storage.object.v1.finalized
  transport:
    pubsub:
      topic: projects/my-project/topics/example-topic

This file creates an Eventarc trigger named example-trigger. It listens for new files finalized in Cloud Storage (google.cloud.storage.object.v1.finalized event type). When such an event happens, it sends the event to a Cloud Run service called example-service in the us-central1 region. The trigger uses a Pub/Sub topic example-topic to transport events securely. The serviceAccount defines permissions for the trigger to invoke the Cloud Run service.

Commands
This command creates an Eventarc trigger named 'example-trigger' in the 'us-central1' region. It listens for Cloud Storage file finalized events and sends them to the Cloud Run service 'example-service'. The service account is used to allow Eventarc to call the Cloud Run service.
Terminal
gcloud eventarc triggers create example-trigger --location=us-central1 --destination-run-service=example-service --destination-run-region=us-central1 --matching-criteria=type=google.cloud.storage.object.v1.finalized --service-account=example-service-account@my-project.iam.gserviceaccount.com
Expected OutputExpected
Created trigger [example-trigger].
--location - Specifies the region where the trigger is created.
--destination-run-service - Specifies the Cloud Run service to receive events.
--matching-criteria - Defines which events to listen for.
This command lists all Eventarc triggers in the 'us-central1' region to verify that 'example-trigger' was created successfully.
Terminal
gcloud eventarc triggers list --location=us-central1
Expected OutputExpected
NAME LOCATION DESTINATION example-trigger us-central1 example-service
--location - Filters triggers by region.
This command shows detailed information about the 'example-trigger' to check its configuration and status.
Terminal
gcloud eventarc triggers describe example-trigger --location=us-central1
Expected OutputExpected
name: projects/my-project/locations/us-central1/triggers/example-trigger uid: 12345678-1234-1234-1234-123456789abc createTime: '2024-06-01T12:00:00Z' updateTime: '2024-06-01T12:00:00Z' destination: cloudRunService: service: example-service region: us-central1 matchingCriteria: - attribute: type value: google.cloud.storage.object.v1.finalized serviceAccount: example-service-account@my-project.iam.gserviceaccount.com
--location - Specifies the region of the trigger.
Key Concept

If you remember nothing else from this pattern, remember: Eventarc connects events from cloud services to your apps automatically and securely.

Common Mistakes
Not specifying the correct event type in matching criteria.
The trigger will not listen to the events you expect, so your app won't react.
Use the exact event type string like 'google.cloud.storage.object.v1.finalized' for Cloud Storage file uploads.
Using a service account without proper permissions.
Eventarc cannot invoke the destination service, causing failures.
Ensure the service account has the 'roles/run.invoker' role on the Cloud Run service.
Creating the trigger in a different region than the Cloud Run service.
Eventarc triggers must be in the same region as the destination service.
Match the trigger's --location with the Cloud Run service's region.
Summary
Create an Eventarc trigger to listen for specific cloud events and route them to a Cloud Run service.
Verify the trigger creation by listing and describing it with gcloud commands.
Use correct event types, service accounts, and regions to ensure the trigger works properly.