HTTP Cloud Run 関数のスケジュールを設定する

このチュートリアルでは、Cloud Scheduler で HTTP エンドポイントをターゲットにして、HTTP Cloud Run 関数をスケジュールする方法について説明します。関数で URL エンドポイントを使用し、Webhook などの HTTP リクエストに応答する必要がある場合は、 HTTP 関数 を使用します。

または、CloudEvents に基づいてイベント ドリブン関数を作成する場合は、Eventarc トリガーを使用して、プロジェクト内のイベントに応答するように関数をスケジュールします。Eventarc は、Pub/Sub トピックのメッセージ、Cloud Storage バケットの変更などをトリガーできます。詳細については、 Cloud Run 関数を作成するをご覧ください。

Cloud Run 関数の実行のスケジューリングは、Cloud Scheduler の一般的なユースケースです。このチュートリアルの内容は次のとおりです。

  1. HTTP Cloud Run 関数を作成する
  2. Cloud Scheduler ジョブを作成する
  3. Cloud Scheduler ジョブを実行する
  4. Cloud Run 関数で結果を確認する

費用

このドキュメントでは、課金対象である次のコンポーネントを使用します。 Google Cloud

料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。

新規の Google Cloud ユーザーは無料トライアルをご利用いただける場合があります。

始める前に

  1. アカウントにログインします。 Google Cloud を初めて使用する場合は、 アカウントを作成して、実際のシナリオで Google プロダクトのパフォーマンスを評価してください。 Google Cloud新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. 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.

  4. Verify that billing is enabled for your Google Cloud project.

  5. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  6. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. 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.

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

  7. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. 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.

  9. Verify that billing is enabled for your Google Cloud project.

  10. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. Create a service account:

    1. 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.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. 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.

    5. Click Create and continue.
    6. 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.

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

Cloud Run 関数では、基盤となる Cloud Run サービスを管理することで呼び出し権限を使用できるため、 Cloud Run 起動元ロールを付与する必要があります。

必要なロール

チュートリアルを完了するために必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。

ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。

ロールを付与する

コンソール

  1. コンソールで、[IAM] ページに移動します。 Google Cloud

    [IAM] に移動
  2. プロジェクトを選択します。
  3. [アクセスを許可] をクリックします。
  4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。これは通常、Cloud Run サービスのデプロイに使用されるメールアドレスです。

  5. [ロールを選択] リストでロールを選択します。
  6. 追加のロールを付与するには、[ 別のロールを追加] をクリックして各ロールを追加します。
  7. [保存] をクリックします。

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 関数を作成します。

  1. コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud

    [サービス] に移動

  2. [関数を作成] をクリックします。

  3. [構成] セクションで、次の操作を行います。

    1. [サービス名] フィールドに、関数の名前を入力します。
    2. [リージョン] リストでリージョンを選択します。
    3. デフォルトのランタイム言語は変更しないでください。
    4. 認証のために、[Require authentication] を選択します。

      ドメイン制限の組織のポリシーでプロジェクトの 未認証呼び出しが制限されている場合は、限定公開サービスの テストの説明に従って、デプロイされたサービスにアクセスする必要があります。

      詳細については、 認証の概要をご覧ください。

  4. その他のデフォルト値はそのままにして、[作成] をクリックします。

  5. 関数の HTTP URL をコピーします。

Terraform を使用して Cloud Run 関数をデプロイする方法など、詳細については、 Cloud Run 関数をデプロイするをご覧ください。

Cloud Scheduler ジョブを作成する

HTTP を使用してアクションをトリガーする Cloud Scheduler ジョブを作成します。

  1. コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud

    [ジョブ] に移動

  2. [ Create job] をクリックします

  3. ジョブの名前を入力します。

  4. [リージョン] リストでリージョンを選択します。

  5. ジョブの頻度を unix-cron 形式で指定します。

    30 16 * * 7
    

    詳細については、 cron ジョブの形式とタイムゾーンをご覧ください。

  6. [タイムゾーン] リストで、タイムゾーンを選択します。

  7. [続行] をクリックします。

  8. [ターゲット タイプ] リストで、[HTTP] を選択します。

  9. [URL] フィールドに、先ほどコピーした関数の HTTP URL を入力します。

  10. [Auth ヘッダー] リストで、[OIDC トークンを追加] を選択します。

  11. [サービス アカウント] リストで、先ほど作成したサービス アカウントを選択します。

  12. [オーディエンス] フィールドに、先ほどコピーした関数の HTTP URL を入力します。

  13. [作成] をクリックします。

日曜日の 16:30 に Cloud Run 関数を実行する cron ジョブが作成されました。

Cloud Scheduler ジョブを実行する

作成したジョブを実行できるようになりました。

  1. コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud

    [ジョブ] に移動

  2. 作成したジョブのチェックボックスをオンにします。

  3. [Force run] をクリックします。

    初めて呼び出すときは、プロジェクトで作成された最初のジョブの構成と実行に数分かかることがあります。

    ジョブが実行されると、[最後の実行のステータス] に Success と表示されます。

Cloud Run 関数で結果を確認する

Cloud Run 関数が cron ジョブによって正常にトリガーされ、実行されていることを確認できます。

  1. コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud

    [サービス] に移動

  2. 関数のサービスリストをフィルタするには、 [filter_list] [Filter > Deployment type > Function] を選択します。

  3. ファンクション名をクリックします。

    [サービスの詳細] ページが開き、サービス指標が表示されます。

  4. [Logs] タブをクリックします。

    POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME のようなログエントリが表示されます。

クリーンアップ

このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

プロジェクトの削除

  1. コンソールで [**リソースの管理**] ページに移動します。 Google Cloud

    [リソースの管理] に移動

  2. プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
  3. ダイアログでプロジェクト ID を入力し、 [Shut down] をクリックしてプロジェクトを削除します。

チュートリアル リソースの削除

  1. コンソールで、[**Cloud Scheduler**] > [**ジョブ**] ページに移動します。 Google Cloud

    [ジョブ] に移動

  2. ジョブの横のチェックボックスを選択します。

  3. [ Delete] をクリックして、 削除を確定します。

  4. コンソールで、[Cloud Run] [>] [サービス] ページに移動します。 Google Cloud

    [サービス] に移動

  5. サービスの横にあるチェックボックスをオンにします。

  6. [ Delete] をクリックして、 削除を確定します。

  7. コンソールで、[サービス アカウント] ページに移動します。 Google Cloud

    [サービス アカウント] に移動

  8. 作成したサービス アカウントの横にあるチェックボックスをオンにします。

  9. [ 削除] をクリックして、削除を確定します。

次のステップ