このチュートリアルでは、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コンポーネントを使用します。
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
始める前に
- 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.
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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, 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 ソース デベロッパー(
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 に移動 - プロジェクトを選択します。
- [アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。これは通常、Cloud Run サービスのデプロイに使用されるメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、[ 別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
- PROJECT_NUMBER: Google Cloud プロジェクト番号。
- PROJECT_ID: 実際の Google Cloud プロジェクト ID。
- PRINCIPAL: バインディングを追加するアカウント。これは通常、Cloud Run サービスのデプロイに使用されるメールアドレスです。
- ROLE: デプロイするアカウントに付与するロール。
Google Cloud コンソールで、[Cloud Run] ページに移動します。
[関数を作成] をクリックします。
[構成] セクションで、次の操作を行います。
- [サービス名] フィールドに、関数の名前を入力します。
- [リージョン] リストでリージョンを選択します。
- デフォルトのランタイム言語は変更しないでください。
認証のために、[Require authentication] を選択します。
ドメイン制限の組織のポリシーでプロジェクトの未認証呼び出しが制限されている場合は、限定公開サービスのテストの説明に従って、デプロイされたサービスにアクセスする必要があります。
詳細については、呼び出しの認証をご覧ください。
その他のデフォルト値はそのままにして、[作成] をクリックします。
関数の HTTP URL をコピーします。
Google Cloud コンソールで [Cloud Scheduler] ページに移動します。
[ジョブを作成] をクリックします。
ジョブの名前を入力します。
[リージョン] リストでリージョンを選択します。
ジョブの頻度を unix-cron 形式で指定します。
30 16 * * 7詳細については、cron ジョブの形式とタイムゾーンをご覧ください。
[タイムゾーン] リストで、タイムゾーンを選択します。
[続行] をクリックします。
[ターゲット タイプ] リストで、[HTTP] を選択します。
[URL] フィールドに、前にコピーした関数の HTTP URL を入力します。
[Auth ヘッダー] リストで、[OIDC トークンを追加] を選択します。
[サービス アカウント] リストで、以前に作成したサービス アカウントを選択します。
[Audience] フィールドに、前にコピーした関数の HTTP URL を入力します。
[作成] をクリックします。
Google Cloud コンソールで、[Cloud Scheduler] ページに移動します。
作成したジョブのチェックボックスをオンにして、[強制実行] をクリックします。
初めて呼び出すときは、プロジェクトで作成された最初のジョブの構成と実行に数分かかることがあります。
ジョブの実行後、[最後の実行のステータス] に
Successが表示されます。Google Cloud コンソールで、[Cloud Run] ページに移動します。
[サービスをフィルタ > デプロイタイプ > 関数] を選択します。これにより、サービスリストがフィルタされ、関数のみが表示されます。
ファンクション名をクリックします。
[サービスの詳細] ページが開き、サービス指標が表示されます。
[Logs] タブをクリックします。
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAMEのようなログエントリが表示されます。- 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 コンソールで、[Cloud Run] ページに移動します。
サービスの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
Google Cloud コンソールで、[サービス アカウント] ページに移動します。
作成したサービス アカウントの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
- Cloud Scheduler を使用して、スケジュールに従って Cloud Run をトリガーする
- CloudEvents 関数を作成して、プロジェクト内のイベントに応答するように関数をスケジュール設定します。
Cloud Run 関数では、基盤となる Cloud Run サービスを管理することで呼び出し権限を使用できるため、Cloud Run 起動元ロールを付与する必要があります。
必要なロール
チュートリアルを完了するために必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。
ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
ロールを付与する
コンソール
gcloud
プロジェクトで自分のアカウントに必要な IAM ロールを付与するには:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL \ --role=ROLE
次のように置き換えます。
HTTP Cloud Run 関数を作成する
Cloud Scheduler によってトリガーされる HTTP 関数を作成します。
Terraform を使用して Cloud Run functions をデプロイする方法など、詳細については、Cloud Run functions の関数をデプロイするをご覧ください。
Cloud Scheduler ジョブを作成する
HTTP を使用してアクションをトリガーする Cloud Scheduler ジョブを作成します。
日曜日の 16:30 に Cloud Run functions の関数を実行する cron ジョブが作成されました。
Cloud Scheduler ジョブを実行する
作成したジョブを実行できるようになりました。
Cloud Run 関数で結果を確認する
Cloud Run 関数が cron ジョブによって正常にトリガーされ、実行されていることを確認できます。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。