Eventarc: Trigger Cloud Run ด้วย Cloud Storage ผ่าน Eventarc แบบมันๆ

Redbulls Thailand
Google Cloud Thailand
4 min readApr 28, 2023

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

ก่อนหน้านี้เราได้ลอง Trigger Cloud Run ด้วย Cloud Pub/Sub ผ่าน Eventarc ไปแล้ว อาจจะยังไม่สาแก่ใจ ขั้นต่อไปเรามาลอง Trigger Cloud Run ด้วย Cloud Storage ผ่าน Eventarc ดู. ท่านี้น่าสนใจมากเพราะมีหลายๆ Use Case จริงใช้เทคนิคการวางไฟล์ไว้บน Cloud Storage Bucket แล้วนำไฟล์ดังกล่าวไป process ในทันที เช่นการเรื่อง Order/Transaction, Worflow ต่างๆ หรือ แม้กระทั่ง ML development

DEMO Steps:

Demo Architecture: Cloud Storage Trigger Cloud Run via Eventarc

ขั้นแรก: เตรียม Pre-reqให้พร้อม

#############################################################
# Pre-requisite Step:
#############################################################
# Enable required Google Cloud API services
gcloud services enable eventarc.googleapis.com
gcloud services enable run.googleapis.com

# ประกาศ Environment Variables และ ใช้ GCP project ปัจจุบัน
REGION=us-central1
SERVICE_NAME=hello
PROJECT_ID=$(gcloud config get-value project)
SERVICE_ACCOUNT=eventarc-trigger-sa

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

gcloud iam service-accounts create $SERVICE_ACCOUNT

ขั้นที่สอง: สร้าง Eventarc ตาม Demo Architecture



#############################################################
# Main Step: Trigger Cloud Run ด้วย Cloud Storage ผ่าน Eventarc
#############################################################

# ประกาศ Environment Variables ที่จำเป็นต้องการใช้ Cloud Storage & Eventarc Trigger
BUCKET_NAME=eventarc-gcs-$PROJECT_ID
SERVICE_ACCOUNT_STORAGE=$(gsutil kms serviceaccount -p $PROJECT_ID)
TRIGGER_NAME=trigger-storage

# สร้าง Cloud Storage Bucket และ อนุญาติให้ Default Service Account ของ Cloud Storage สามารถ Publish message ลง Pub/Sub ได้
gsutil mb -l $REGION gs://$BUCKET_NAME
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT_STORAGE \
--role roles/pubsub.publisher

# อนุญาติให้ Service Account ของ Eventarc สามารถรับ Event ได้
gcloud projects add-iam-policy-binding $PROJECT_ID \
--role roles/eventarc.eventReceiver \
--member serviceAccount:$SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com

# สร้าง Eventarc Trigger
# จาก Event Type จาก Cloud Storage เมื่อไหร่ก็ตามที่ file มีการเปลี่ยนแปลงใน Bucket
# แล้วให้ Trigger Target คือ Hello World Cloud Run
# โดยอนุญาติให้ใช้ Service Account ของ Eventarc Trigger ได้เท่านั้น

gcloud eventarc triggers create $TRIGGER_NAME \
--destination-run-service=$SERVICE_NAME \
--destination-run-region=$REGION \
--event-filters="type=google.cloud.storage.object.v1.finalized" \
--event-filters="bucket=$BUCKET_NAME" \
--location=$REGION \
--service-account=$SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com

Credited: Trigger Cloud Run with Eventarc events

เมื่อสร้าง Eventarc Trigger เรียบร้อยแล้ว, ลองมาดูว่าเกิดอะไรขึ้นบ้าง เริ่มต้นจาก Cloud Pub/Sub ก่อนเรย. อย่างที่บอกว่าทุกครั้งที่มีการสร้าง Eventarc Trigger, มันจะสร้าง Cloud Pub/Sub Topic/Subscription ต่อหนึ่ง Trigger ให้อัตโนมัติ เพื่อใช้รับส่ง Event จากต้นทางไป Trigger ปลายทาง (ง่ายจริง)

Cloud Pub/Sub Topic/Subscription created by Eventarc

ส่วนที่เพิ่มเติมขึ้นมาสำหรับกรณีใช้ Cloud Storage เป็น Event Type/Source นั้นคือเรื่อง สิทธิ์ที่ต้องให้เพิ่มเติมกับตัว Default Service Account ของ Cloud Storage เพื่อให้มันสามารถ Publish Storage Event ใดๆได้บน Topic ที่ Eventarc สร้างขึ้นมาให้นั่นเอง.

Note: เราไม่จำเป็นต้องใช้ Default Service Account ของ Cloud Storage ก็ได้น่ะ ถ้าเรามี custom service account อยู่ปแล้ว

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

Cloud Run Trigger Configuration created by Eventarc

บน Eventarc Console ก็จะเห็น Resoources และ Configurations ใดๆเกี่ยวข้องว่า ต้นทางเป็น Event Type ไหน ปลายทางไป Trigger ใคร. สำหรับกรณีนี้ Event Provider ก็คือ Cloud Storage จาก Bucket ที่ระบุเท่านั้น (Bucket อื่นไม่มีผลนะ) แล้วไป Trigger Target คือ Cloud Run

ที่อยากจะอธิบายเพิ่มเติม 2 จุด

  • Event Type = google.cloud.storage.object.v1.finalized ​ซึ่งจากเอกสารของ Google Cloud ทำให้เรารู้ว่าเป็นการเจาะจง Event ที่เกี่ยวข้องกับ การ Create/Update object ใน Cloud Storage เท่านั้น (delete object, หรือ แก้ไข metadata ของ object จะไม่ถูกส่งเข้ามาใน Eventarc)
  • Infrastructure = Direct, ซึ่งก็คือ Event Type ประเภทนี้เป็น Native Event ยิงตรงเข้าหัวใจ Eventarc โดยไม่จำต้องใช้เท่า Cloud Audit Logs
Supported Cloud Storage Event Types

ขั้นสุดท้าย: เริ่มทดสอบ

อธิบายมายืดยาว ถึงเวลาทดสอบซะที!!! ไม่เห็นจะยาก สร้าง file แล้ว copy ผ่าน gsutil ขึ้น bucket ปลาย

echo "Hello World" > random.txt
gsutil cp random.txt gs://$BUCKET_NAME/random.txt

รอลุ้นอึดใจเดียว Eventarc ก็ Trigger Cloud Run แล้วก็ส่ง CloudEvent ที่ภายใน Payload ก็มี Object URL/URI ของ random.txt มาให้เรียบร้อย

สรุป

ใน Demo Architecture เป็นการต่อยอดการใช้ Event Type ที่มาจาก ​Cloud Storage โดยเลือกเฉพาะเมื่อมีการ create/update file/object ใน Cloud Storage เท่านั้น. Event Type ตัวนี้เป็นแบบ Direct Event ซึ่งต่อตรงเข้า Eventarc ได้เรย (Native Event) และ Direct Event แบบนี้ฟรีอีกด้วย เมื่อเทียบเมื่อใช้ท่าผ่าน Cloud Audit Logs ซึ่งอาจจะมีค่าใช้จ่ายเพิ่มโดยเฉพาะถ้าต้อง Enable Data Access Audit Logs. และเมื่อสร้าง Eventarc Trigger มันก็จะสร้าง Cloud Pub/Sub Topic/Subscription เฉพาะสำหรับตัวนี้ให้ เพื่อไว้รับ Event จากต้นทางแล้วส่งต่อหรือ Trigger Cloud Run ปลายทางในรูป CloudEvent ซึ่งเนื้อของ Payload จะมี Cloud Storage Object URL/URL ที่เกี่ยวข้อง.

ท้ายนี้หวังว่า “Dev สายซิ่ง” ทุกคนจะสามารถประยุกต์ใช้ Use Case นี้ผ่าน Eventarc ได้กับหลายๆ Applications เช่นพวก Workflow ต่างๆ.

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

--

--