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 Scheduler ジョブが Cloud Run 関数をトリガーすることを確認します。

費用

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

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

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

始める前に

  1. 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.
  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. Verify that billing is enabled for your Google Cloud project.

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

    Enable the APIs

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

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

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

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

    Enable the APIs

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

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

    必要なロール

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

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

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

    ロールを付与する

    コンソール

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

      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. Google Cloud コンソールで、[Cloud Run] ページに移動します。

      Cloud Run に移動

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

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

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

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

        詳細については、呼び出しの認証をご覧ください。

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

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

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

    Cloud Scheduler ジョブを作成する

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

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

      Cloud Scheduler に移動

    2. [ジョブを作成] をクリックします。

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

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

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

      30 16 * * 7
      

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

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

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

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

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

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

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

    12. [Audience] フィールドに、前にコピーした関数の HTTP URL を入力します。

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

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

    Cloud Scheduler ジョブを実行する

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

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

      Cloud Scheduler に移動

    2. 作成したジョブのチェックボックスをオンにして、[強制実行] をクリックします。

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

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

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

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

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

      Cloud Run に移動

    2. [サービスをフィルタ > デプロイタイプ > 関数] を選択します。これにより、サービスリストがフィルタされ、関数のみが表示されます。

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

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

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

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

    クリーンアップ

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

    プロジェクトの削除

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

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

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

      Cloud Scheduler に移動

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

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

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

      Cloud Run に移動

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

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

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

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

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

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

    次のステップ