Patrick
Patrick

Reputation: 8083

Cloud Run Jobs events to trigger Cloud Function with EventArc

I set up a Terraform google_cloudfunctions2_function with the following trigger config:

event_trigger {
    trigger_region        = var.region
    event_type            = "google.cloud.audit.log.v1.written"
    retry_policy          = "RETRY_POLICY_RETRY"
    service_account_email = google_service_account.event.email

    event_filters {
      attribute = "serviceName"
      value     = "run.googleapis.com"
    }

    event_filters {
      attribute = "methodName"
      value     = "/Jobs.RunJob"
    }
...

If I query the Cloud Logs for:

protoPayload.serviceName="run.googleapis.com"
protoPayload.methodName="/Jobs.RunJob"

I can see the events there, but they're not triggering my Cloud Function. Roles are all there:

- roles/eventarc.eventReceiver
- roles/run.invoker
- roles/cloudfunctions.invoker

screenshot of trigger

What am I doing wrong?

What's odd is that this is all v1 in my Audit Logs. The job is created as google_cloud_run_v2_job in Terraform and I explicitly added execution_environment = "EXECUTION_ENVIRONMENT_GEN2", but still v1.

In the Cloud Logs, I can find/filter by those parameters for the events I want to use as a trigger:gcp screenshot

Upvotes: 0

Views: 365

Answers (3)

Patrick
Patrick

Reputation: 8083

I can't believe it is that difficult to trigger something when a Cloud Run Job is done! There should be a simple event already on PubSub just like Cloud Build.

None of the Eventarc methods worked and we subscribed to paid Google Cloud support to get this resolved. The answer is a Log Sink with the following filter:

resource.type = cloud_run_job 
protoPayload.status.message =~ Execution.*.has completed successfully

With PubSub as destination.

Upvotes: 0

KikoZam
KikoZam

Reputation: 419

Make sure that methodName is in the correct format:

Try changing the methodName from /Jobs.RunJob to google.cloud.run.v2.Jobs.RunJob

Updated trigger config:

event_filters {
      attribute = "methodName"
      value     = "google.cloud.run.v2.Jobs.RunJob"    
}

Check with Google Cloud Logs Explorer to see if Cloud Run Jobs are being logged under v2 or v1 methods. Otherwise, use google.cloud.run.v1.Jobs.RunJob as the method name in the event filter.

Upvotes: 1

guillaume blaquiere
guillaume blaquiere

Reputation: 75970

In addition to my comment (you have to activate the audit logs to get this event with eventarc), I manually defined an eventarc trigger on the UI and I didn't get the same config as you

enter image description here

I did it because the /Jobs.RunJob sounded odd for me!

Upvotes: 1

Related Questions