排定事件導向的 Cloud Run 函式


本教學課程將示範如何使用 Cloud Scheduler,並以 Pub/Sub 主題為目標,觸發事件驅動的 Cloud Run 函式。如果函式應直接因應 Google Cloud 專案中的事件觸發,例如 Pub/Sub 主題上的訊息或 Cloud Storage 值區中的變更,請使用事件驅動函式。

請注意,您也可以指定函式的 HTTP 端點,排定 HTTP 函式。如果需要函式具備網址端點並回應 HTTP 要求 (例如 Webhook),請使用 HTTP 函式。詳情請參閱「Cloud Run 函式類型」。

排定 Cloud Run 函式的執行作業是 Cloud Scheduler 的常見用途。在這個教學課程中,您將執行下列操作:

  1. 建立訂閱 Pub/Sub 主題的簡單 Cloud Run 函式。
  2. 建立 Cloud Scheduler 工作,將訊息發布至 Pub/Sub 主題。
  3. 執行 Cloud Scheduler 工作。
  4. 確認 Cloud Run 函式是否由 Cloud Scheduler 工作觸發。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  10. 請注意,您必須授予 Cloud Run 叫用者角色,因為在 Cloud Run 函式 (第 2 代) 中,您可以管理基礎 Cloud Run 服務,取得叫用權限。

  11. 根據預設,Cloud Run 函式會使用系統自動建立的預設 Compute Engine 服務帳戶做為執行階段服務帳戶。您可以使用這個服務帳戶試用本教學課程。不過,視貴機構的政策設定而定,預設服務帳戶可能不會自動取得專案的編輯者角色。如果是這種情況,您必須將下列角色授予服務帳戶
    1. Artifact Registry 寫入者 (roles/artifactregistry.writer)
    2. 記錄寫入者 (roles/logging.logWriter)
    3. Storage 物件檢視者 (roles/storage.objectViewer)
  12. 建立事件導向的 Cloud Run 函式

    建立事件驅動函式,直接回應 Google Cloud 專案中的事件 (在本例中,是發布至 Pub/Sub 主題的訊息)。

    1. 前往 Google Cloud 控制台的「Cloud Run functions」頁面。

      前往 Cloud Run functions

    2. 按一下「建立函式」

    3. 在「Basics」(基本) 部分執行下列操作:

      1. 在「Environment」(環境) 清單中選取「2nd gen」(第 2 代)
      2. 輸入函式名稱。
      3. 在「Region」(區域) 清單中選取區域。
    4. 在「觸發條件」部分執行下列操作:

      1. 在「Trigger type」(觸發條件類型) 清單中,選取「Cloud Pub/Sub」
      2. 在「Cloud Pub/Sub topic」(Cloud Pub/Sub 主題) 清單中,選取現有主題,或點選「Create a topic」(建立主題) 建立新主題。請記下主題名稱,後續步驟會用到。
      3. 按一下 [更多選項]

        「Eventarc trigger」(Eventarc 觸發條件) 面板隨即開啟。

    5. 在「Eventarc trigger」(Eventarc 觸發條件) 面板中,執行下列操作:

      1. 如果系統提示您允許 Pub/Sub 服務建立叫用函式所需的身分識別權杖,請按一下「授權」
      2. 在「Service account」(服務帳戶) 清單中,選取您先前建立的服務帳戶。
      3. 接受其他預設值。
      4. 按一下「儲存觸發條件」

        「Eventarc trigger」(Eventarc 觸發條件) 面板會關閉。

    6. 接受其他預設值,然後按一下「下一步」

    7. 請勿修改預設執行階段語言和範例程式碼。程式碼不會使用任何雲端服務,也不需要啟用其他權限。按一下 [Deploy] (部署)

    建立 Cloud Scheduler 工作

    建立以 Pub/Sub 為目標的 Cloud Scheduler 工作。

    1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

      前往 Cloud Scheduler

    2. 按一下「建立工作」

    3. 輸入工作名稱。

    4. 在「Region」(區域) 清單中選取區域。

    5. 使用 unix-cron 格式指定工作的頻率

      30 16 * * 7
      

      詳情請參閱「Cron 工作格式和時區」。

    6. 在「時區」清單中選取時區。

    7. 按一下「繼續」

    8. 在「Target type」(目標類型) 清單中,選取「Pub/Sub」

    9. 選取您先前建立的 Pub/Sub 主題。

    10. 「Message body」(訊息內文) 欄位中,輸入要傳送至 Pub/Sub 目標主題的字串,例如:「Hello world!」

    11. 點選「建立」

    您已建立 Cron 工作,會在星期日 16:30 將訊息傳送至 Pub/Sub 主題。您的 Cloud Run 函式已訂閱該主題。

    執行 Cloud Scheduler 工作

    現在可以執行您建立的工作。

    1. 前往 Google Cloud 控制台的 Cloud Scheduler 頁面。

      前往 Cloud Scheduler

    2. 選取您建立的工作核取方塊,然後按一下「強制執行」

      首次叫用時,專案中建立的第一項工作可能需要幾分鐘的時間才能設定及執行。

    3. 工作執行後,「上次執行狀態」應會顯示 Success

    驗證 Cloud Run 函式中的結果

    您可以確認 Cron 工作是否已成功觸發及執行 Cloud Run 函式。

    1. 前往 Google Cloud 控制台的「Cloud Run functions」頁面。

      前往 Cloud Run functions

    2. 按一下函式名稱。

      「Function details」(函式詳細資料) 頁面隨即會開啟,「Invocations/Second」(每秒呼叫次數) 圖表會顯示函式的第一次呼叫。

    3. 按一下 [Logs] (記錄) 分頁標籤。

      您應該會看到類似 Hello, YOUR_STRING! 的記錄項目

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

    刪除專案

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    刪除教學課程資源

    1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

      前往 Cloud Scheduler

    2. 選取工作旁的核取方塊。

    3. 按一下「刪除」,並確認要刪除該項目。

    4. 前往 Google Cloud 控制台的「Pub/Sub」頁面。

      前往 Pub/Sub

    5. 勾選主題旁的核取方塊。

    6. 按一下「刪除」,並確認要刪除該項目。

    7. 前往 Google Cloud 控制台的「Cloud Run functions」頁面。

      前往 Cloud Run functions

    8. 選取函式旁邊的核取方塊。

    9. 按一下「刪除」,並確認要刪除該項目。

    10. 前往 Google Cloud 控制台的「Service Accounts」(服務帳戶) 頁面。

      前往「Service Accounts」(服務帳戶)

    11. 找出您建立的服務帳戶,然後選取旁邊的核取方塊。

    12. 按一下「刪除」,並確認要刪除該項目。

    後續步驟