# Eventarc A trigger that delivers events to a Cloud Run service: one for messages on a Pub/Sub topic, one for objects finalised in a bucket. - A trigger has its own service account. It needs `roles/eventarc.eventReceiver` on the project and `roles/run.invoker` on the service it calls. - For a Pub/Sub trigger, name an existing topic under `transport.pubsub.topic`; left out, Eventarc creates a topic you do not manage. - For a Cloud Storage trigger, `location` must be the bucket's location, and the Cloud Storage service agent needs `roles/pubsub.publisher` on the project before the trigger is created. - `matching_criteria` attributes are exact. `type` is always present; a storage event also needs `bucket`. - The destination service is named, with its `region`; `path` is optional. - The service must not allow unauthenticated calls for this to be the only way in: grant `roles/run.invoker` to the trigger's service account only. ```hcl variable "project_id" { type = string description = "Project to deploy into." } variable "region" { type = string description = "Region for regional resources." } variable "service_name" { type = string description = "Name of the Cloud Run service that receives the events." } variable "topic_id" { type = string description = "Id of the Pub/Sub topic whose messages are delivered." } variable "bucket_name" { type = string description = "Bucket whose new objects are delivered. It must be in var.region." } data "google_storage_project_service_account" "this" { project = var.project_id } resource "google_service_account" "trigger" { project = var.project_id account_id = "eventarc-trigger" display_name = "Eventarc trigger" } resource "google_project_iam_member" "trigger_receives_events" { project = var.project_id role = "roles/eventarc.eventReceiver" member = "serviceAccount:${google_service_account.trigger.email}" } resource "google_cloud_run_v2_service_iam_member" "trigger_invokes_service" { project = var.project_id location = var.region name = var.service_name role = "roles/run.invoker" member = "serviceAccount:${google_service_account.trigger.email}" } resource "google_project_iam_member" "storage_publishes_events" { project = var.project_id role = "roles/pubsub.publisher" member = "serviceAccount:${data.google_storage_project_service_account.this.email_address}" } resource "google_eventarc_trigger" "on_message" { project = var.project_id name = "on-message" location = var.region service_account = google_service_account.trigger.email matching_criteria { attribute = "type" value = "google.cloud.pubsub.topic.v1.messagePublished" } transport { pubsub { topic = var.topic_id } } destination { cloud_run_service { service = var.service_name region = var.region path = "/events/message" } } depends_on = [ google_project_iam_member.trigger_receives_events, google_cloud_run_v2_service_iam_member.trigger_invokes_service, ] } resource "google_eventarc_trigger" "on_object" { project = var.project_id name = "on-object" location = var.region service_account = google_service_account.trigger.email matching_criteria { attribute = "type" value = "google.cloud.storage.object.v1.finalized" } matching_criteria { attribute = "bucket" value = var.bucket_name } destination { cloud_run_service { service = var.service_name region = var.region path = "/events/object" } } depends_on = [ google_project_iam_member.trigger_receives_events, google_project_iam_member.storage_publishes_events, google_cloud_run_v2_service_iam_member.trigger_invokes_service, ] } ```