排定 HTTP Cloud Run 函式


本教學課程說明如何透過 Cloud Scheduler 中的 HTTP 端點,排定 HTTP Cloud Run 函式的執行時間。如果需要函式具備網址端點並回應 HTTP 要求 (例如 Webhook),請使用 HTTP 函式

或者,如果您建立了 CloudEvents 函式,請使用 Eventarc 事件觸發條件,排定函式回應專案內事件的時間。Eventarc 可觸發 Pub/Sub 主題的訊息、Cloud Storage 值區的變更等。詳情請參閱Cloud Run functions 類型

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

  1. 建立簡單的 HTTP Cloud Run 函式。
  2. 建立 Cloud Scheduler 工作。
  3. 執行 Cloud Scheduler 工作。
  4. 確認 Cloud Scheduler 工作是否會觸發 Cloud Run 函式。

費用

在本文件中,您會使用 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 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 following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    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 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 following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

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

  10. 請注意,您必須授予 Cloud Run 叫用者角色,因為在 Cloud Run 函式中,叫用權限是透過管理基礎 Cloud Run 服務取得。

    必要的角色

    如要取得完成本教學課程所需的權限,請要求管理員為您授予專案的下列 IAM 角色:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

    您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

    授予角色

    控制台

    1. 前往 Google Cloud 控制台的「IAM」(身分與存取權管理)IAM 頁面。

      前往 IAM
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中輸入使用者 ID。這通常是部署 Cloud Run 服務時使用的 Google 帳戶電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [儲存]

    gcloud

    如要在專案中授予帳戶必要的 IAM 角色,請按照下列步驟操作:

         gcloud projects add-iam-policy-binding PROJECT_ID \
             --member=PRINCIPAL \
             --role=ROLE
         

    取代:

    • PROJECT_NUMBER 改成您的 Google Cloud 專案編號。
    • PROJECT_ID 改成您的專案 ID。 Google Cloud
    • PRINCIPAL,並為該帳戶新增繫結。這通常是部署 Cloud Run 服務時使用的 Google 帳戶電子郵件地址。
    • ROLE,並將角色新增至部署者帳戶。

    建立 HTTP Cloud Run 函式

    建立由 Cloud Scheduler 觸發的 HTTP 函式。

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

      前往 Cloud Run

    2. 按一下「編寫函式」

    3. 在「設定」部分,完成下列步驟:

      1. 在「Service name」(服務名稱) 欄位中,輸入函式的名稱。
      2. 在「Region」(區域) 清單中選取區域。
      3. 請勿修改預設執行階段語言。
      4. 如要進行驗證,請選取「需要驗證」

        如果專案受到網域限制組織政策限制,無法進行未經驗證的呼叫,您必須按照「測試私人服務」一節的說明,存取已部署的服務。

        詳情請參閱「驗證以進行叫用」。

    4. 接受其他預設值,然後按一下「建立」

    5. 複製函式的 HTTP 網址。

    建立 Cloud Scheduler 工作

    建立 Cloud Scheduler 工作,透過 HTTP 觸發動作。

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

      前往 Cloud Scheduler

    2. 按一下「建立工作」

    3. 輸入工作名稱。

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

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

      30 16 * * 7
      

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

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

    7. 按一下「繼續」

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

    9. 在「URL」欄位中,輸入您先前複製的函式 HTTP 網址。

    10. 在「Auth header」(授權標頭) 清單中,選取「Add OIDC token」(新增 OIDC 權杖)

    11. 在「Service account」(服務帳戶) 清單中,選取您先前建立的服務帳戶。

    12. 在「對象」欄位中,輸入您先前複製的函式 HTTP 網址。

    13. 點選「建立」

    您已建立 Cron 工作,會在星期日 16:30 執行 Cloud Run 函式。

    執行 Cloud Scheduler 工作

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

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

      前往 Cloud Scheduler

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

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

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

    驗證 Cloud Run 函式中的結果

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

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

      前往 Cloud Run

    2. 依序選取「篩選服務」>「部署類型」>「函式」。這會篩選服務清單,只顯示函式。

    3. 按一下函式名稱。

      「服務詳細資料」頁面隨即開啟,並顯示服務指標。

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

      您應該會看到類似以下的記錄項目: POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME

    清除所用資源

    如要避免系統向您的 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 控制台的「Cloud Run」頁面。

      前往 Cloud Run

    5. 選取服務旁的核取方塊。

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

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

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

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

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

    後續步驟