環境の設定

cron ジョブをスケジュールして実行する前に、Cloud Scheduler 用に Google Cloudプロジェクトと環境を設定する必要があります。

Cloud Scheduler API を有効にします

次の手順に沿って、 Google Cloud プロジェクトを作成または選択し、Cloud Scheduler API を有効にします。

コンソール

  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 (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 Scheduler API.

    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 API

  5. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  7. Enable the Cloud Scheduler API.

    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 API

gcloud

  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. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Cloud Scheduler API:

    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.

    gcloud services enable cloudscheduler.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  13. Enable the Cloud Scheduler API:

    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.

    gcloud services enable cloudscheduler.googleapis.com

認証を設定する

Cloud Scheduler に対する認証方法は、API へのアクセスに使用するインターフェースと、コードが実行されている環境によって異なります。

詳細については、Cloud Scheduler に対して認証するHTTP ターゲットで認証を使用するをご覧ください。

App Engine アプリケーションを追加する

App Engine HTTP ターゲットを使用する場合は、プロジェクトに App Engine アプリケーションを追加する必要があります。このアプリは、Cloud Scheduler サービスの場所とジョブハンドラ自体の両方として機能します。ジョブハンドラは、Cloud Scheduler ジョブに関連付けられた処理を実行し、応答で確認応答を返します。

プロジェクトにない App Engine ジョブ ハンドラを使用する場合は、代わりに HTTP ターゲットを選択する必要があります。この場合、プロジェクトに App Engine アプリは必要ありません。

コンソール

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

    App Engine に移動

  2. [App Engine へようこそ] ダイアログで、次のいずれかを行います。

    • App Engine アプリケーションをすでに作成していて、[App Engine アプリケーションが作成されました] というメッセージが表示されている場合は、このセクションの残りの手順をスキップできます。

      または

    • App Engine アプリケーションをまだ作成していない場合は、[アプリケーションを作成] をクリックして、このセクションの残りの手順に進みます。

  3. アプリケーションのリージョンを選択します。

    europe-westus-central は、Cloud Scheduler コマンドではそれぞれ europe-west1us-central1 と呼ばれます。

    App Engine アプリのリージョンを設定した後は、変更できません

  4. サービス アカウントを選択しない。デフォルトの App Engine サービス アカウントが使用されます。

  5. [次へ] をクリックします。

    アプリケーションが構成され、作成されます。これには数分かかることがあります。

  6. Cloud SDK はダウンロードせずに、[後で行う] をクリックします。

    App Engine アプリケーションが作成されました」というメッセージが表示されます。

gcloud

  1. App Engine アプリを作成するには、gcloud app create コマンドを実行します。

    gcloud app create --region=REGION
    

    REGION は、アプリが実行されるロケーションに置き換えます。europe-westus-central は、Cloud Scheduler コマンドではそれぞれ europe-west1us-central1 と呼ばれます。

    App Engine アプリのリージョンを設定した後は、変更できません

  2. 次のコマンドで既存のアプリのリージョンを確認できます。

    gcloud app describe
    

    locationId はリージョンを示します。例: locationId: us-central