Try   HackMD

GCP - Cloud Functions (需要授權才能觸發)

tags: Cloud

參考: Authenticating for invocation

Use Cases (參考 Use Cases)

A. Data processing / ETL: 針對Cloud Storage的事件做回應,以及處理圖片、視頻轉碼、驗證
B. WebHook: 可藉由網路Http(s) 回應第三方事件、ex: github, gitlab
C. Lightweight APIs: 輕量級的程式,直接透過Http(s)進行調用
D. Mobile backend: 整合GCP提供的後台服務(firebase, storage,etc.)
E. IoT: 透過Cloud Pub/Sub,進行資料處理、轉換,再推送給另一個處理平台

Cloud Run Funcions vs Cloud Run Job

1. 先選用專案,這裡我使用預設專案 My First Project

Go to project Dashboard

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. 先開通Google Function API

開通API

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3. 在左邊的導航中,點取Cloud Funtions

到Cloud Funtions選單中,點選CREATE FUNCTION

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定Cloud Function參數,若只是要設定,可以直接選擇Allow unauthenticated invocations

  • 2nd gen 開發使用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定完成後,按下next,這時候可以選擇要使用什麼程式語言。這裡我們選擇使用inline Editor的方式直接建立,也可以使用上傳java專案的方式,要記的的更新Entry point,完成後建立就OK了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

創建成功

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

4. 測試Cloud Functions

4.1 在Cloud Function葉面上,選擇function,按下PERMISSIONS。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

4.2 按下Add principals,將個人帳戶mail或service mail加入倒pricipals中,設定腳色為Cloud Functions Invoker。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

4.3 使用developer方式進行權限驗證

gcloud auth login --update-adc

登入成功

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

4.4 找出Token

gcloud auth print-identity-token

Cloud Functions Dashoard按下選擇的Function, 在Triiger頁面中,複製URL

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最終將4.4與4.5步驟加入POSTMAN中

成功

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

無token觸發會失敗

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →