このチュートリアルでは、Cloud Scheduler を使用して、Pub/Sub トピックをターゲットに設定し、Cloud Run のイベント ドリブン関数をトリガーする方法について説明します。Pub/Sub トピックのメッセージや Cloud Storage バケットの変更など、 Google Cloud プロジェクト内のイベントに応答して関数を直接トリガーする場合は、イベント ドリブン関数を使用します。
関数の HTTP エンドポイントをターゲットにして、HTTP 関数の実行スケジュールを設定することもできます。関数で URL エンドポイントを使用し、Webhook などの HTTP リクエストに応答する必要がある場合は、HTTP 関数を使用します。詳細については、Cloud Run 関数のタイプをご覧ください。
Cloud Run 関数の実行のスケジューリングは、Cloud Scheduler の一般的なユースケースです。このチュートリアルの内容は次のとおりです。
- Pub/Sub トピックをサブスクライブする単純な Cloud Run 関数を作成する。
- Pub/Sub トピックにメッセージを公開する Cloud Scheduler ジョブを作成する。
- Cloud Scheduler ジョブを実行する。
- 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, Eventarc 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 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.
- 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, Eventarc 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 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.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
- デフォルトでは、Cloud Run functions は、自動的に作成されるデフォルトの Compute Engine サービス アカウントをランタイム サービス アカウントとして使用します。このサービス アカウントを使用して、このチュートリアルを試すことができます。ただし、組織のポリシーの構成によっては、デフォルトのサービス アカウントにプロジェクトの編集者のロールが自動的に付与されない場合があります。その場合、次のロールをサービス アカウントに付与する必要があります。
- Artifact Registry 書き込み(
roles/artifactregistry.writer) - ログ書き込み(
roles/logging.logWriter) - Storage オブジェクト閲覧者(
roles/storage.objectViewer)
- Artifact Registry 書き込み(
Google Cloud コンソールで、[Cloud Run functions] ページに移動します。
[関数を作成] をクリックします。
[基本] セクションで、次の操作を行います。
- [環境] リストで [第 2 世代] を選択します。
- 関数の名前を入力します。
- [リージョン] リストでリージョンを選択します。
[トリガー] セクションで、次の操作を行います。
- [トリガーのタイプ] リストで [Cloud Pub/Sub] を選択します。
- [Cloud Pub/Sub トピック] リストで、既存のトピックを選択するか、[トピックを作成] をクリックして新しいトピックを作成します。後の手順で必要になるため、トピックの名前をメモしておきます。
[その他のオプション] をクリックします。
[Eventarc トリガー] パネルが開きます。
[Eventarc トリガー] パネルで、次の操作を行います。
- ファンクションの呼び出しに必要な ID トークンを Pub/Sub サービスが作成できるようにするよう求められたら、[付与] をクリックします。
- [サービス アカウント] リストで、以前に作成したサービス アカウントを選択します。
- 他の値はデフォルトを使用します。
[トリガーを保存] をクリックします。
[Eventarc トリガー] パネルが閉じます。
その他のデフォルト値はそのままにして、[次へ] をクリックします。
デフォルトのランタイム言語とサンプルコードは変更しないでください。このコードはクラウド サービスを使用せず、追加の権限を有効にする必要もありません。[デプロイ] をクリックします。
Google Cloud コンソールで [Cloud Scheduler] ページに移動します。
[ジョブを作成] をクリックします。
ジョブの名前を入力します。
[リージョン] リストでリージョンを選択します。
ジョブの頻度を unix-cron 形式で指定します。
30 16 * * 7詳細については、cron ジョブの形式とタイムゾーンをご覧ください。
[タイムゾーン] リストで、タイムゾーンを選択します。
[続行] をクリックします。
[ターゲット タイプ] リストで、Pub/Sub を選択します。
先ほど作成した Pub/Sub トピックを選択します。
[メッセージ本文] フィールドに、Pub/Sub ターゲット トピックに送信する文字列を入力します。例: 「Hello world!」
[作成] をクリックします。
Google Cloud コンソールで、[Cloud Scheduler] ページに移動します。
作成したジョブのチェックボックスをオンにして、[強制実行] をクリックします。
初めて呼び出すときは、プロジェクトで作成された最初のジョブの構成と実行に数分かかることがあります。
ジョブの実行後、[最後の実行のステータス] に
Successが表示されます。Google Cloud コンソールで、Cloud Run functions ページに移動します。
ファンクション名をクリックします。
[関数の詳細] ページが開き、[Invocations/Second] グラフに関数の最初の呼び出しが表示されます。
[Logs] タブをクリックします。
Hello, YOUR_STRING!のようなログエントリが表示されます。- 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 コンソールで、[Pub/Sub] ページに移動します。
トピックの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
Google Cloud コンソールで、Cloud Run functions ページに移動します。
ファンクションの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
Google Cloud コンソールで、[サービス アカウント] ページに移動します。
作成したサービス アカウントの横にあるチェックボックスをオンにします。
[ 削除] をクリックして、削除を確定します。
Cloud Run 関数(第 2 世代)では、基盤となる Cloud Run サービスを管理することで呼び出し権限を使用できるため、Cloud Run 起動元ロールを付与する必要があります。
Cloud Run のイベント ドリブン関数を作成する
Google Cloud プロジェクト内のイベント(この場合は Pub/Sub トピックに公開されたメッセージ)に応答して直接トリガーされるイベント ドリブン関数を作成します。
Cloud Scheduler ジョブを作成する
Pub/Sub ターゲットを使用して Cloud Scheduler ジョブを作成します。
日曜日の 16:30 に Pub/Sub トピックにメッセージを送信する cron ジョブが作成されました。Cloud Run 関数は、このトピックにサブスクライブされています。
Cloud Scheduler ジョブを実行する
作成したジョブを実行できるようになりました。
Cloud Run 関数で結果を確認する
Cloud Run 関数が cron ジョブによって正常にトリガーされ、実行されていることを確認できます。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。