Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Eventarc: Trigger Cloud Run services with event...

Eventarc: Trigger Cloud Run services with events from Google Cloud

Eventarc is a eventing product that allows you to trigger Cloud Run services from Google Cloud sources. In this talk, we'll get a tour of Eventarc features and see some event-driven Cloud Run demos powered by Eventarc.

Mete Atamel

April 15, 2021
Tweet

More Decks by Mete Atamel

Other Decks in Programming

Transcript

  1. Proprietary + Confidential A new way of getting events to

    managed Cloud Run services Private alpha since early 2020 as Events for Cloud Run Public beta since Oct 2020 as Eventarc GA since Q1 2021 Eventarc
  2. Proprietary + Confidential An image is added to Cloud Storage

    bucket → Cloud Run uses Vision API to analyze the image A BigQuery job is completed → Cloud Run sends an email notification using SendGrid A Compute Engine VM is created → Cloud Run adds a label the VM Some use cases
  3. Proprietary + Confidential Eventarc with its AuditLog and Pub/Sub integration

    potentially allows more integration Eventarc’s trigger is a higher level abstraction Users can manage all their triggers in one place in Eventarc Eventarc vs. Pub/Sub
  4. Proprietary + Confidential Create a trigger for new object creation

    in Cloud Storage → Cloud Run gcloud eventarc triggers create trigger-auditlog \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName=storage.googleapis.com" \ --event-filters="methodName=storage.objects.create" \ --event-filters=${PROJECT_NO}[email protected] gcloud - AuditLog
  5. Proprietary + Confidential Create a trigger for Pub/Sub messages →

    Cloud Run gcloud eventarc triggers create trigger-pubsub \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" --transport-topic=projects/${PROJECT_ID}/topics/${TOPIC_ID} gcloud - Pub/Sub
  6. POST / HTTP/1.1 Content-Type: application/json; charset=utf-8 Content-Length: 33 ce-specversion: 1.0

    ce-type: google.cloud.pubsub.topic.publish ce-time: 2020-09-05T03:56:24Z ce-id: 1234-1234-1234 ce-source: mycontext/subcontext custom-attr: 42 { "message": "Hello Cloud Next!" } CloudEvents cloudevents.io "Data" "Context"
  7. Cloud Run Service CloudEvent SDK HTTP request → CloudEvent in

    your language cloudevents.io Google Events Library Type library for CloudEvent#data Event parsing libraries HTTP POST to Run URL HTTP body is a CloudEvent with event data. ("binary" CloudEvent V1) (optional) (optional) github.com/googleapis/google-cloudevents
  8. const { HTTP } = require("cloudevents"); const {toLogEntryData} = require('@google/events/cloud/audit/v1/LogEntryData')

    app.post('/', async (req, res) => { // Read CloudEvent using CloudEvents SDK const cloudEvent = HTTP.toEvent({ headers: req.headers, body: req.body }); // Read AuditLog using Google.Events library for Node.js const logEntryData = toLogEntryData(cloudEvent.data); // Extract bucket and objectName const tokens = logEntryData.protoPayload.resourceName.split('/'); const bucket = tokens[3]; const objectName = tokens[5];
  9. using CloudNative.CloudEvents; using Google.Events; using Google.Events.Protobuf.Cloud.PubSub.V1; public async Task<CloudEvent> Read(HttpContext

    context) { // Read CloudEvent using CloudEvents SDK var cloudEvent = await context.Request.ReadCloudEventAsync(); // Read Pub/Sub message using Google.Events library for .NET var messagePublishedData = CloudEventConverters.ConvertCloudEventData<MessagePublishedData>(cloudEvent); // Extract the Pub/Sub message var pubSubMessage = messagePublishedData.Message;
  10. Proprietary + Confidential Quickstart cloud.google.com/eventarc Codelab: Trigger Cloud Run with

    events from Eventarc codelabs.developers.google.com/codelabs/cloud-run-events Thank you