本教學課程說明如何指定 Cloud Scheduler 中的 HTTP 端點,排定 HTTP Cloud Run 函式的執行時間。如果函式需要網址端點並回應 HTTP 要求 (例如 Webhook),請使用 HTTP 函式。
或者,如果您根據 CloudEvents 建立事件導向函式,請使用 Eventarc 觸發條件,排定函式回應專案中的事件。Eventarc 可觸發 Pub/Sub 主題訊息、Cloud Storage bucket 變更等。詳情請參閱「編寫 Cloud Run 函式」。
安排 Cloud Run 函式的執行作業是 Cloud Scheduler 的常見用途。在這個教學課程中,您將執行下列操作:
費用
在本文件中,您會使用下列 Google Cloud計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
事前準備
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build, Cloud Run Admin, Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
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 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.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
請注意,您必須授予 Cloud Run 叫用者角色,因為在 Cloud Run functions 中,叫用權限是透過管理基礎 Cloud Run 服務來提供。
必要的角色
如要取得完成教學課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
- Cloud Run 來源開發人員 (
roles/run.sourceDeveloper) - Cloud Scheduler 管理員 (
roles/cloudscheduler.admin) - 記錄檔檢視存取者 (
roles/logging.viewAccessor) - 專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin) - 服務帳戶管理員 (
roles/iam.serviceAccountAdmin) - 服務帳戶使用者 (
roles/iam.serviceAccountUser)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
授予角色
控制台
-
前往 Google Cloud 控制台的「IAM」頁面。
前往「IAM」頁面 - 選取專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。通常是部署 Cloud Run 服務時使用的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下 [儲存]。
gcloud
如要將必要的 IAM 角色授予專案中的帳戶,請按照下列步驟操作:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL \ --role=ROLE
更改項目:
- PROJECT_NUMBER 替換為您的 Google Cloud 專案編號。
- 將 PROJECT_ID 替換為 Google Cloud 專案 ID。
- PRINCIPAL 替換為您要新增繫結的帳戶。通常是部署 Cloud Run 服務時使用的電子郵件地址。
- ROLE:要新增至部署者帳戶的角色。
建立 HTTP Cloud Run 函式
建立由 Cloud Scheduler 觸發的 HTTP 函式。
在 Google Cloud 控制台,依序前往「Cloud Run」>「Services」(服務) 頁面。
點選「編寫函式」。
在「Configure」(設定) 部分,執行下列操作:
其餘設定均採用預設值,然後點選「建立」。
複製函式的 HTTP 網址。
如要瞭解如何使用 Terraform 部署 Cloud Run 函式等資訊,請參閱「部署 Cloud Run 函式」。
建立 Cloud Scheduler 工作
建立 Cloud Scheduler 工作,使用 HTTP 觸發動作。
在 Google Cloud 控制台,依序前往「Cloud Scheduler」>「Jobs」(工作) 頁面。
按一下「Create job」(建立工作)。
輸入工作名稱。
在「Region」(區域) 清單中選取區域。
使用 unix-cron 格式指定工作頻率:
30 16 * * 7詳情請參閱「Cron 工作格式和時區」。
在「Timezone」(時區) 清單中選取時區。
按一下「繼續」。
在「Target type」(目標類型) 清單中,選取「HTTP」。
在「URL」欄位中,輸入先前複製的函式 HTTP 網址。
在「Auth header」(驗證標頭) 清單中,選取「Add OIDC token」(新增 OIDC 權杖)。
在「Service account」(服務帳戶) 清單中,選取您先前建立的服務帳戶。
在「Audience」(目標對象) 欄位中,輸入先前複製的函式 HTTP 網址。
點選「建立」。
您已建立 Cron 工作,會在週日下午 4:30 執行 Cloud Run 函式。如要讓 HTTP 函式具備冪等性,並在 Cloud Scheduler 重試工作時防止重複處理,請參閱「簡化工作要求」。
執行 Cloud Scheduler 工作
現在可以執行建立的工作。
在 Google Cloud 控制台,依序前往「Cloud Scheduler」>「Jobs」(工作) 頁面。
勾選您建立的工作。
按一下 「Force run」(強制執行)。
首次叫用時,在專案中建立的第一項工作可能需要幾分鐘的時間才能設定及執行。
工作執行後,「Status of last execution」(上次執行狀態) 應顯示
Success。
在 Cloud Run functions 中驗證結果
您可以確認 Cloud Run 函式是否成功由 Cron 工作觸發並執行。
在 Google Cloud 控制台中,依序前往「Cloud Run」>「Services」(服務) 頁面。
如要篩選函式服務清單,請依序選取「filter_list」filter_list「篩選器」>「部署類型」>「函式」。
按一下函式名稱。
「Service details」(服務詳細資料) 頁面隨即開啟,並顯示服務指標。
點選「記錄」分頁標籤。
您應該會看到類似以下的記錄項目:
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除教學課程資源
在 Google Cloud 控制台,依序前往「Cloud Scheduler」>「Jobs」(工作) 頁面。
勾選工作旁的核取方塊。
按一下「 刪除」並確認刪除。
在 Google Cloud 控制台中,依序前往「Cloud Run」>「Services」(服務) 頁面。
勾選服務旁的核取方塊。
按一下「 刪除」並確認刪除。
前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。
找出您建立的服務帳戶,然後勾選旁邊的核取方塊。
按一下「 刪除」並確認刪除。
後續步驟
- 簡化工作要求
- 使用 Cloud Scheduler 依排程觸發 Cloud Run
- 編寫 CloudEvents 函式,排定函式回應專案中的事件。