Files
gcp_solution_architecture_a…/skills/google-cloud-iac-patterns/references/eventarc.md
Jonathan Boniface 71da0ed504
Some checks failed
validation / verify (push) Failing after 12s
chore: housekeeping from the runs of 2026-10-09
- An Execution's files are written as each Deliverable is submitted, marked
  in_progress, so a killed process or one waiting for its user leaves them on
  disk. A write removes files an earlier write left that it no longer has.
- A question about delivered work is answered from that Execution's solution
  guide by the reply writer; a question about the service is answered as
  before. Nothing is started for either.
- Lessons: prove writes only for lessons the Skill holds; the Skill records
  the vendored Skill versions the lessons were read against, status names any
  that changed, and a test fails until they are reviewed.
- Four more code patterns: Firestore, BigQuery, Eventarc and GKE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:44:38 +01:00

130 lines
3.8 KiB
Markdown

# 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,
]
}
```