このチュートリアルでは、Cloud Scheduler で HTTP エンドポイントをターゲットにして、HTTP Cloud Run 関数をスケジュールする方法について説明します。関数で URL エンドポイントを使用し、Webhook などの HTTP リクエストに応答する必要がある場合は、 HTTP 関数 を使用します。
または、CloudEvents に基づいてイベント ドリブン関数を作成する場合は、Eventarc トリガーを使用して、プロジェクト内のイベントに応答するように関数をスケジュールします。Eventarc は、Pub/Sub トピックのメッセージ、Cloud Storage バケットの変更などをトリガーできます。詳細については、 Cloud Run 関数を作成するをご覧ください。
Cloud Run 関数の実行のスケジューリングは、Cloud Scheduler の一般的なユースケースです。このチュートリアルの内容は次のとおりです。
費用
このドキュメントでは、課金対象である次のコンポーネントを使用します。 Google Cloud
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
- アカウントにログインします。 Google Cloud を初めて使用する場合は、 アカウントを作成して、実際のシナリオで Google プロダクトのパフォーマンスを評価してください。 Google Cloud新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
-
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.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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
(
-
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.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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 サービスを管理することで呼び出し権限を使用できるため、 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)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
ロールを付与する
コンソール
-
コンソールで、[IAM] ページに移動します。 Google Cloud
[IAM] に移動 - プロジェクトを選択します。
- [アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。これは通常、Cloud Run サービスのデプロイに使用されるメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、[ 別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
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 関数を作成します。
コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud
[関数を作成] をクリックします。
[構成] セクションで、次の操作を行います。
その他のデフォルト値はそのままにして、[作成] をクリックします。
関数の HTTP URL をコピーします。
Terraform を使用して Cloud Run 関数をデプロイする方法など、詳細については、 Cloud Run 関数をデプロイするをご覧ください。
Cloud Scheduler ジョブを作成する
HTTP を使用してアクションをトリガーする Cloud Scheduler ジョブを作成します。
コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud
[ Create job] をクリックします。
ジョブの名前を入力します。
[リージョン] リストでリージョンを選択します。
ジョブの頻度を unix-cron 形式で指定します。
30 16 * * 7詳細については、 cron ジョブの形式とタイムゾーンをご覧ください。
[タイムゾーン] リストで、タイムゾーンを選択します。
[続行] をクリックします。
[ターゲット タイプ] リストで、[HTTP] を選択します。
[URL] フィールドに、先ほどコピーした関数の HTTP URL を入力します。
[Auth ヘッダー] リストで、[OIDC トークンを追加] を選択します。
[サービス アカウント] リストで、先ほど作成したサービス アカウントを選択します。
[オーディエンス] フィールドに、先ほどコピーした関数の HTTP URL を入力します。
[作成] をクリックします。
日曜日の 16:30 に Cloud Run 関数を実行する cron ジョブが作成されました。
Cloud Scheduler ジョブを実行する
作成したジョブを実行できるようになりました。
コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud
作成したジョブのチェックボックスをオンにします。
[Force run] をクリックします。
初めて呼び出すときは、プロジェクトで作成された最初のジョブの構成と実行に数分かかることがあります。
ジョブが実行されると、[最後の実行のステータス] に
Successと表示されます。
Cloud Run 関数で結果を確認する
Cloud Run 関数が cron ジョブによって正常にトリガーされ、実行されていることを確認できます。
コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud
関数のサービスリストをフィルタするには、 [filter_list] [Filter > Deployment type > Function] を選択します。
ファンクション名をクリックします。
[サービスの詳細] ページが開き、サービス指標が表示されます。
[Logs] タブをクリックします。
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAMEのようなログエントリが表示されます。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
プロジェクトの削除
- コンソールで [**リソースの管理**] ページに移動します。 Google Cloud
- プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
- ダイアログでプロジェクト ID を入力し、 [Shut down] をクリックしてプロジェクトを削除します。
チュートリアル リソースの削除
コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud
ジョブの横のチェックボックスを選択します。
[ Delete] をクリックして、 削除を確定します。
コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud
サービスの横にあるチェックボックスをオンにします。
[ Delete] をクリックして、 削除を確定します。
コンソールで、[サービス アカウント] ページに移動します。 Google Cloud
作成したサービス アカウントの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
次のステップ
- Cloud Scheduler を使用して、スケジュールに沿って Cloud Run をトリガーする
- CloudEvents 関数を作成して、 プロジェクト内のイベントに応答するように関数をスケジュールする。