Eventarc: Trigger Cloud Run ด้วย Pub/Sub ผ่าน Eventarc แบบ Fun Fun

Redbulls Thailand
Google Cloud Thailand
4 min readApr 26, 2023

#event-driven #eventarc #pubsub #integration #appdev

ความเดิมจากตอนที่แล้ว Eventarc: Messaging Services สำหรับ Dev สายซิ่ง

“สิบปากว่าก็ไม่เท่าตาเห็น สิบตาเห็นก็ไม่เท่าลงมือทำ” เชื่อว่า “Dev สายซิ่ง” หลายคนก็คงเริ่มคันไม้คันมือกันบ้าง ซึ่ง Part นี้ก็จะเน้นลงมือทำจริงๆสักที จะได้เห็นภาพว่า Eventarc มันทำงานยังงัย เชื่อมต่อกับแบบไหน แล้ว เราจะนำไปประยุกต์ใช้ต่ออย่างไร

ง่ายๆ เพียงแค่ 3 ขั้นตอน เรามาเริ่มกันเลย +++

DEMO Steps:

ขั้นแรก: ก่อนที่เราจะสร้าง Trigger ผ่าน Eventarc นั้น เราต้องรู้ก่อนว่าจะเลือกหยิบ Event จาก Sources ไหน และ Types อะไร ที่เราสนใจไป Trigger App ของเรา, ซึ่งเราสามารถใช้ “gcloud beta eventarc attributes types list” เพื่อดู Event Types ทั้งหมดได้

gcloud beta eventarc attributes types list

NAME: google.cloud.audit.log.v1.written
DESCRIPTION: Cloud Audit Log: Sent when a log is written.

NAME: google.cloud.pubsub.topic.v1.messagePublished
DESCRIPTION: Cloud Pub/Sub: Sent when a message is published.

NAME: google.cloud.storage.object.v1.archived
DESCRIPTION: Cloud Storage: Sent when a live version of an (object versioned) object is archived or deleted.

NAME: google.cloud.storage.object.v1.deleted
DESCRIPTION: Cloud Storage: Sent when an object has been permanently deleted.

NAME: google.cloud.storage.object.v1.finalized
DESCRIPTION: Cloud Storage: Sent when a new object (or a new generation of an existing object).

NAME: google.cloud.storage.object.v1.metadataUpdated
DESCRIPTION: Cloud Storage: Sent when the metadata of an existing object changes.

Results จาก command ดังกล่าว จะแสดง 3 Sources จาก Google Cloud เท่านั้น คือ google.cloud.audit.log, google.cloud.pubsub, และ google.cloud.storage ซึ่งแต่ละ Sources อาจจะมีรายละเอียด Types มากกว่า 1 เช่น google.cloud.storage จะมี Event Types — object.v1.archived, deleted, finalized, metadataUpdated เป็นต้น.

ตัว Event Type=google.cloud.audit.log.v1.written เป็นตัว common ของหลาย Google Cloud Services เป็นการใช้ความสามารถของ Cloud Audit Logs ซึ่งจะคอย​ record ทุกๆ Methods บน Google Cloud Services. เราสามารถใช้ “gcloud beta eventarc attributes service-names list — type=<EVENT_TYPE>” เพื่อดูว่ามี Google Cloud Services ตัวไหนที่รองรับบ้าง จากนั้นก็ซูมเข้าไปราย Service ว่ามี Methods อะไรบ้างด้วย “gcloud beta eventarc attributes method-names list — type=<EVENT_TYPE> — service-name=<SERVICE_NAME>

# ​List all Google services ที่รองรับด้วย Event Type จาก cloud audit logs
gcloud beta eventarc attributes service-names list --type=google.cloud.audit.log.v1.written

SERVICE_NAME: accessapproval.googleapis.com
DISPLAY_NAME: Access Approval
SERVICE_NAME: accesscontextmanager.googleapis.com
DISPLAY_NAME: Access Context Manager
SERVICE_NAME: admin.googleapis.com
DISPLAY_NAME: Google Workspace Admin
SERVICE_NAME: aiplatform.googleapis.com
DISPLAY_NAME: AI Platform (under Vertex AI)
...

# ​List all Methods (Operations) ที่รองรับภายใต้ Service นั้นบน Event Type จาก cloud audit logs
gcloud beta eventarc attributes method-names list --type=google.cloud.audit.log.v1.written --service-name=pubsub.googleapis.com
METHOD_NAME: google.iam.v1.IAMPolicy.GetIamPolicy
METHOD_NAME: google.iam.v1.IAMPolicy.SetIamPolicy
METHOD_NAME: google.pubsub.v1.Publisher.CreateTopic
METHOD_NAME: google.pubsub.v1.Publisher.DeleteTopic
METHOD_NAME: google.pubsub.v1.Publisher.DetachSubscription
METHOD_NAME: google.pubsub.v1.Publisher.GetTopic
...

ขั้นที่สอง: เมื่อเรารู้ Event Type ไหนที่เราสนใจแล้ว ก็มาเริ่มสร้าง Evenarc แบบง่ายๆกัน โดย Use Case ตัวอย่างจะเป็นการ Trigger Cloud Run ผ่าน Eventarc เมื่อไหร่ก็ตามที่มี Message ถูกใส่ลงมาใน Cloud Pub/Sub ตามรูปด้านล่าง

Demo Architecture: Pub/Sub Trigger Cloud Run via Eventarc

โดยมีแค่ 3 ขั้นตอนเท่านั้นดังนี้:

  • Step1: Deploy Hello World Cloud Run Application เพื่อใช้เป็น Trigger Target สำหรับ Eventarc
  • Step2: สร้าง Service Account ที่ Eventarc เอาไว้ใช้ในการอ่าน Message จาก Cloud Pub/Sub แล้ว Trigger Cloud Run
  • Step:3 Deploy Eventarc Trigger โดยใช้ (Source) Event Type จาก Cloud Pub/Sub และ Target เป็น Cloud Run
# Enable required Google Cloud API services
gcloud services enable eventarc.googleapis.com
gcloud services enable run.googleapis.com

# ประกาศ Environment Variables
REGION=us-central1
SERVICE_NAME=hello
TRIGGER_NAME=trigger-pubsub
SERVICE_ACCOUNT=eventarc-trigger-sa

# Step1: Deploy Hello World Cloud Run เพื่อไว้ใช้เป็น Eventarc Target
gcloud run deploy $SERVICE_NAME \
--allow-unauthenticated \
--image=gcr.io/cloudrun/hello \
--region=$REGION

# Step2: สร้าง Service Account สำหรับ Eventarc ที่จะใช้ อ่าน message จาก Pub/Sub และ Trigger Cloud Run
gcloud iam service-accounts create $SERVICE_ACCOUNT

# Step3: Deploy Eventarc Trigger โดยใช้ Event Type จาก pubsub และ Target เป็น Cloud Run โดยอนุญาตการเข้าถึงด้วย Service Account
gcloud eventarc triggers create $TRIGGER_NAME \
--destination-run-service=$SERVICE_NAME \
--destination-run-region=$REGION \
--event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
--location=$REGION \
--service-account=$SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com
Creating trigger [trigger-pubsub] in project [xxxxxx], location [us-central1]...done.
Created Pub/Sub topic [projects/xxxxxx/topics/eventarc-us-central1-trigger-pubsub-037].
Publish to this topic to receive events in Cloud Run service [hello].
WARNING: It may take up to 2 minutes for the new trigger to become active.

Credited: Trigger Cloud Run with Eventarc events

ตอนเราสร้าง Eventarc Trigger (Step3)เรียบร้อยแล้ว, เราจะเห็นว่า Eventarc จะช่วยสร้าง Cloud Pub/Sub Topic และ Subscription ให้อัตโนมัติ (ถ้าเป็นอดีตที่มีเฉพาะ Cloud Pub/Sub เราต้องมาจัดการตรงนี้เอง รวมถึงเรื่อง Permission ต่างๆ)

Cloud Pub/Sub Topic/Subscription created by Eventarc

และบน Cloud Run ก็จะผูก Eventarc Trigger ให้อัตโนมัติ และ อนุญาติให้เฉพาะ Service Account ที่ใช้กับ Eventarc สามารถ Trigger Cloud Run ตัวนี้ได้อย่างเดียว

Cloud Run Trigger Configuration created by Eventarc

ถ้าเราไปดูเมนู Eventarc ก็จะเห็นรายละเอียด Configurations ต่างๆที่สร้างไว้

Eventarc Configurations & Menu

ขั้นสุดท้าย: ทำการทดสอบโดยการ push sample pub/sub message ลงใน Topic ที่ถูกสร้างโดย Eventrc แล้วดูว่า Cloud Run ได้รับ Trigger หรือไม่ ซึ่งเราจะดูได้จาก Cloud Logging

# Retrieve Cloud Pub/Sub topic ที่ถูกสร้างโดย Eventarc
TOPIC_ID=$(gcloud eventarc triggers describe $TRIGGER_NAME --location $REGION --format='value(transport.pubsub.topic)')

# จำลองส่ง message ลงเข้าไปใน Topic ดังกล่าว
gcloud pubsub topics publish $TOPIC_ID --message="Hello World"

เข้าไปดู Menu Cloud Run → LOG (ซึ่งก็จะดึง Logs ของ Cloud Run ตัวนี้มาจาก Cloud Logging) เราก็จะเห็นว่ามี Message “Hello World” เข้ามาผ่าน Event Type: google.cloud.pubusb.topic.v1.messagePublished แล้วก็มีการ Trigger Cloud Run จริงๆ (POST 200) เย้+++

สรุป

ใน Demo Architecture ซึ่งเป็นโครงสร้างพื้นฐานของ Event-Driven Application ทั่วไป ที่มีการใช้ Cloud Pub/Sub เป็นตัวรับ/ส่ง Messages ผ่าน Topic/Subscription. ซึ่งปกติแล้วถ้าไม่มี Eventarc, Application ต้อง subscribe โดยตรงกับ Cloud Pub/Sub Subscription. แต่ถ้าใช้ Eventarc, Application ก็ไม่ต้องทำ Subscription อีกต่อไป แค่เปลี่ยนตัวเองมาเป็นการรอรับ Trigger จาก Eventarc แทน ซึ่งก้อน Data จะถูกส่งผ่าน Payload ใน CloudEvent มาให้ด้วย

การสร้าง Resources และ Configurations ต่างๆ ถูก simplify/abstract ด้วย Eventarc เพียง command เดียว (gcloud eventarc triggers create)!!! แน่นอนว่าชีวิต “Dev สายซิ่ง” จะสบายขึ้น, พัฒนา Application ได้รวดเร็ว, และ ปวดหัวน้อยลง

แต่เดี๋ยวก่อนนี่เป็นแค่หนึ่งในท่าที่ Eventarc รองรับ ยังมีทาง Event Types มาจาก Cloud Storage และ Cloud Audit Logs อีกน่ะ โปรดติดตามตอนต่อไป…..

ติดตามต่อได้:

--

--