起動後のスクリプトを使用する

このガイドでは、Colab Enterprise で起動後スクリプトを使用する方法について説明します。このガイドでは、起動後スクリプトで使用する環境変数を設定する方法についても説明します。

概要

起動後スクリプトを使用すると、Colab Enterprise ランタイムの起動プロセスの完了後にタスクを実行できます。たとえば、起動後のスクリプトを使用して、特定のパッケージをインストールしたり、ランタイムの VM に特定の変更を加えたりできます。

Colab Enterprise で起動後スクリプトを使用するには、ランタイム テンプレートを作成するときに起動後スクリプトを指定します。そのランタイム テンプレートをベースにしてランタイムを作成すると、起動プロセスの終了時に起動後スクリプトが実行されます。

起動後のスクリプトの場所

起動後のスクリプトの URI は次のいずれかになります。

  • Cloud Storage URI: たとえば、gs://BUCKET_NAME/SCRIPT_FILE_NAME。起動後のスクリプトが Cloud Storage に保存されている場合は、エンドユーザー認証情報が有効になっているランタイムを使用する必要があります。これは、Colab Enterprise がユーザー認証情報を使用して Cloud Storage バケットにアクセスするためです。
  • HTTPS URL: 例: https://example.com/FOLDER_NAME/SCRIPT_FILE_NAME。 Google Cloud コンソールを使用してランタイム テンプレートを作成する場合、HTTPS URL の使用はサポートされていません。

起動後のスクリプトの動作

起動後スクリプトの動作を指定できます。次の表をご覧ください。

動作 説明
1 回実行(RUN_ONCE

デフォルト。Colab Enterprise は、ランタイムの作成時に起動後スクリプトを 1 回だけ実行します。

開始されるたびに実行(RUN_EVERY_START

起動のたびに、Colab Enterprise は起動後スクリプトを実行します。たとえば、アイドル タイムアウトによってランタイムが停止した場合、ランタイムが再起動されると、Colab Enterprise は起動後スクリプトを再度実行します。

開始されるたびにダウンロードして実行(DOWNLOAD_AND_RUN_EVERY_START

起動するたびに、Colab Enterprise は起動後スクリプトをソースから再ダウンロードし、スクリプトを実行します。

環境変数

ランタイム テンプレートで環境変数を指定し、起動後スクリプトでこれらの変数を参照できます。

たとえば、環境変数を使用して割り当てプロジェクトを設定できます。 Google Cloudクライアント ライブラリで使用される共通変数である GOOGLE_CLOUD_QUOTA_PROJECT を使用して、割り当てプロジェクトを特定のプロジェクト ID に設定できます。

環境変数で起動後スクリプトを使用するには、起動後スクリプトと環境変数を使用してランタイム テンプレートを作成するをご覧ください。

起動後スクリプトの変更を追跡して復元する

起動スクリプトの変更を追跡し、特定のバージョンの起動スクリプトの復元を有効にするには、オブジェクトのバージョニングが有効になっている Cloud Storage バケットに起動スクリプトを保存します。オブジェクトのバージョニングでは、削除されたオブジェクトをバージョニングされた非現行バージョンのオブジェクトとして保持します。このオブジェクトは、明示的に削除されるまでバケットでアクセス可能です。

起動後スクリプトのバージョンを指定するには、ランタイム テンプレートを作成するときに、起動後スクリプトの URI に #GENERATION_NUMBER を追加します(例: gs://BUCKET_NAME/FOLDER_NAME/FILE_NAME.py#GENERATION_NUMBER)。オブジェクトのバージョニングと世代番号の詳細については、オブジェクトのバージョニングをご覧ください。

Google Cloud コンソールを使用してランタイム テンプレートを作成する場合、起動後スクリプトの世代番号を指定することはできません。

始める前に

  1. Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Agent Platform, Dataform, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  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 role (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 Agent Platform, Dataform, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

必要なロール

Colab Enterprise でランタイム テンプレートを作成するために必要な権限を取得するには、プロジェクトに対する Colab Enterprise 管理者(roles/aiplatform.colabEnterpriseAdmin)IAM ロールを付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織に対するアクセス権の管理をご覧ください。

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

起動後のスクリプトを使用するランタイム テンプレートを作成する

起動後のスクリプトを使用するランタイム テンプレートを作成するには、 Google Cloud コンソール、Google Cloud CLI、または REST API を使用します。