本教學課程將示範如何使用 Cloud Scheduler,並以 Pub/Sub 主題為目標,觸發事件驅動的 Cloud Run 函式。如果函式應直接因應 Google Cloud 專案中的事件觸發,例如 Pub/Sub 主題上的訊息或 Cloud Storage 值區中的變更,請使用事件驅動函式。
請注意,您也可以指定函式的 HTTP 端點,排定 HTTP 函式。如果需要函式具備網址端點並回應 HTTP 要求 (例如 Webhook),請使用 HTTP 函式。詳情請參閱「Cloud Run 函式類型」。
排定 Cloud Run 函式的執行作業是 Cloud Scheduler 的常見用途。在這個教學課程中,您將執行下列操作:
- 建立訂閱 Pub/Sub 主題的簡單 Cloud Run 函式。
- 建立 Cloud Scheduler 工作,將訊息發布至 Pub/Sub 主題。
- 執行 Cloud Scheduler 工作。
- 確認 Cloud Run 函式是否由 Cloud Scheduler 工作觸發。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
- 根據預設,Cloud Run 函式會使用系統自動建立的預設 Compute Engine 服務帳戶做為執行階段服務帳戶。您可以使用這個服務帳戶試用本教學課程。不過,視貴機構的政策設定而定,預設服務帳戶可能不會自動取得專案的編輯者角色。如果是這種情況,您必須將下列角色授予服務帳戶:
- Artifact Registry 寫入者 (
roles/artifactregistry.writer
) - 記錄寫入者 (
roles/logging.logWriter
) - Storage 物件檢視者 (
roles/storage.objectViewer
)
- Artifact Registry 寫入者 (
前往 Google Cloud 控制台的「Cloud Run functions」頁面。
按一下「建立函式」
。在「Basics」(基本) 部分執行下列操作:
- 在「Environment」(環境) 清單中選取「2nd gen」(第 2 代)。
- 輸入函式名稱。
- 在「Region」(區域) 清單中選取區域。
在「觸發條件」部分執行下列操作:
- 在「Trigger type」(觸發條件類型) 清單中,選取「Cloud Pub/Sub」。
- 在「Cloud Pub/Sub topic」(Cloud Pub/Sub 主題) 清單中,選取現有主題,或點選「Create a topic」(建立主題) 建立新主題。請記下主題名稱,後續步驟會用到。
按一下 [更多選項]。
「Eventarc trigger」(Eventarc 觸發條件) 面板隨即開啟。
在「Eventarc trigger」(Eventarc 觸發條件) 面板中,執行下列操作:
- 如果系統提示您允許 Pub/Sub 服務建立叫用函式所需的身分識別權杖,請按一下「授權」。
- 在「Service account」(服務帳戶) 清單中,選取您先前建立的服務帳戶。
- 接受其他預設值。
按一下「儲存觸發條件」。
「Eventarc trigger」(Eventarc 觸發條件) 面板會關閉。
接受其他預設值,然後按一下「下一步」。
請勿修改預設執行階段語言和範例程式碼。程式碼不會使用任何雲端服務,也不需要啟用其他權限。按一下 [Deploy] (部署)。
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
按一下「建立工作」
。輸入工作名稱。
在「Region」(區域) 清單中選取區域。
使用 unix-cron 格式指定工作的頻率:
30 16 * * 7
詳情請參閱「Cron 工作格式和時區」。
在「時區」清單中選取時區。
按一下「繼續」。
在「Target type」(目標類型) 清單中,選取「Pub/Sub」。
選取您先前建立的 Pub/Sub 主題。
在「Message body」(訊息內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」
點選「建立」。
前往 Google Cloud 控制台的 Cloud Scheduler 頁面。
選取您建立的工作核取方塊,然後按一下「強制執行」。
首次叫用時,專案中建立的第一項工作可能需要幾分鐘的時間才能設定及執行。
工作執行後,「上次執行狀態」應會顯示
Success
。前往 Google Cloud 控制台的「Cloud Run functions」頁面。
按一下函式名稱。
「Function details」(函式詳細資料) 頁面隨即會開啟,「Invocations/Second」(每秒呼叫次數) 圖表會顯示函式的第一次呼叫。
按一下 [Logs] (記錄) 分頁標籤。
您應該會看到類似
Hello, YOUR_STRING!
的記錄項目- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
前往 Google Cloud 控制台的「Cloud Scheduler」頁面。
選取工作旁的核取方塊。
按一下「刪除」
,並確認要刪除該項目。前往 Google Cloud 控制台的「Pub/Sub」頁面。
勾選主題旁的核取方塊。
按一下「刪除」
,並確認要刪除該項目。前往 Google Cloud 控制台的「Cloud Run functions」頁面。
選取函式旁邊的核取方塊。
按一下「刪除」
,並確認要刪除該項目。前往 Google Cloud 控制台的「Service Accounts」(服務帳戶) 頁面。
找出您建立的服務帳戶,然後選取旁邊的核取方塊。
按一下「刪除」
,並確認要刪除該項目。
請注意,您必須授予 Cloud Run 叫用者角色,因為在 Cloud Run 函式 (第 2 代) 中,您可以管理基礎 Cloud Run 服務,取得叫用權限。
建立事件導向的 Cloud Run 函式
建立事件驅動函式,直接回應 Google Cloud 專案中的事件 (在本例中,是發布至 Pub/Sub 主題的訊息)。
建立 Cloud Scheduler 工作
建立以 Pub/Sub 為目標的 Cloud Scheduler 工作。
您已建立 Cron 工作,會在星期日 16:30 將訊息傳送至 Pub/Sub 主題。您的 Cloud Run 函式已訂閱該主題。
執行 Cloud Scheduler 工作
現在可以執行您建立的工作。
驗證 Cloud Run 函式中的結果
您可以確認 Cron 工作是否已成功觸發及執行 Cloud Run 函式。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。