Terraform を使用してユーザー管理ノートブック インスタンスを作成する

このチュートリアルでは、Terraform 構成ファイルを作成し、サービス アカウント モードの Vertex AI でユーザー管理のノートブック インスタンスをプロビジョニングする方法を学習します。

このチュートリアルの手順では、次のツールを使用します。

  • Terraform CLI: Terraform は Cloud Shell にプリインストールされています。Terraform CLI コマンドを使用するために Terraform を個別にインストールする必要はありません。

  • Cloud Shell エディタ: Cloud Shell エディタは、Terraform 構成ファイルの作成と管理、さらにフォルダ構造の管理に使用します。

  • Cloud Shell ターミナル: Cloud Shell ターミナルは、terraform initterraform planterraform applyterraform destroy などの Terraform CLI コマンドを実行するために使用します。

始める前に

Terraform を使用して Vertex AI リソースのインフラストラクチャを構築する前に、Google Cloud プロジェクトと開発環境を設定します。このセクションでは、Terraform がプロジェクト内のリソースを操作する際に使用する Vertex AI 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. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. Vertex AI API を有効にします。

    API を有効にする

  5. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  6. Google Cloud プロジェクトで課金が有効になっていることを確認します

  7. Vertex AI API を有効にします。

    API を有効にする

必要なロール

プロジェクトを作成した場合、そのプロジェクトに対するオーナーroles/owner)IAM ロールが付与されています。このロールには、必要な権限がすべて含まれています。スキップして、次のセクションに進みます。プロジェクトを自分で作成していない場合は、このセクションを続けてください。

Terraform を使用した Vertex AI Workbench ユーザー管理ノートブック インスタンスの作成に必要な権限がユーザー アカウントに付与されるように、プロジェクトに対する次の IAM ロールをユーザー アカウントに付与するよう管理者に依頼してください。

  • ノートブック管理者(roles/notebooks.admin)。Vertex AI Workbench でのインスタンスの作成と削除に使用します。
  • サービス アカウント ユーザー(roles/iam.serviceAccountUser)。サービス アカウントとしてオペレーションを実行するために使用します。このチュートリアルではサービス アカウントを指定しないため、ユーザー管理ノートブック インスタンスでは、デフォルトの Compute Engine サービス アカウントが使用されます。
  • Service Usage ユーザー(roles/serviceusage.serviceUsageConsumer)。サービスの状態とオペレーションの検査、プロジェクト リソースの割り当ての使用と課金に使用します。

ロールの付与の詳細については、アクセスの管理をご覧ください。

管理者は、カスタムロールや他の事前定義ロールを使用して、必要な権限をユーザー アカウントに付与することもできます。

フォルダ構造と Terraform 構成ファイルを作成する

  1. Google Cloud コンソールで、「Cloud Shell をアクティブにする」をクリックします。

    Cloud Shell をアクティブにする

    Google Cloud コンソールの下部で Cloud Shell セッションが開始し、コマンドライン プロンプトが表示されます。Cloud Shell はシェル環境です。Google Cloud CLI がすでにインストールされており、現在のプロジェクトの値もすでに設定されています。セッションが初期化されるまで数秒かかることがあります。

  1. Cloud Shell ターミナルで次のコマンドを実行して、ホーム ディレクトリを Active Directory として設定します。
    cd
  2. 次のコマンドを実行して、terraform という名前の新しいフォルダを作成します。
    mkdir terraform
  3. Cloud Shell エディタを起動する
  4. [Explorer] ペインで、terraform フォルダを右クリックし、[New File] をクリックします。
  5. ファイル名として「main.tf」と入力し、[OK] をクリックします。

Terraform 構成ファイルでインフラストラクチャを定義する

  1. main.tf ファイルを Cloud Shell コードエディタで開きます。

  2. 次の Terraform 構成サンプルの PROJECT_NAMELOCATION のプレースホルダを置き換え、サンプルを main.tf ファイルにコピーします。

    resource "google_project_service" "notebooks" {
      provider           = google
      service            = "notebooks.googleapis.com"
      disable_on_destroy = false
    }
    
    resource "google_notebooks_instance" "basic_instance" {
      project      = "PROJECT_ID"
      name         = "notebooks-instance-basic"
      provider     = google
      location     = "LOCATION"
      machine_type = "e2-medium"
    
      vm_image {
        project      = "deeplearning-platform-release"
        image_family = "tf-ent-2-9-cu113-notebooks"
      }
    
      depends_on = [
        google_project_service.notebooks
      ]
    }
    
    • PROJECT_ID: Google Cloud プロジェクト ID を入力します。

    • LOCATION: ユーザー管理ノートブック インスタンスのリージョンとゾーンを入力します。たとえば、us-west2-b のようにします。最適なネットワーク パフォーマンスを得るため、地理的に最も近いリージョンを選択してください。利用可能なユーザー管理ノートブックのロケーションをご覧ください。

  3. main.tf ファイルを保存します。

Terraform 構成ファイルを含む作業ディレクトリを初期化する

  1. Cloud Shell ターミナルを開くには、Cloud Shell エディタのツールバーで [ターミナルを開く] をクリックします。

  2. Cloud Shell ターミナルで次のコマンドを実行して、terraform フォルダを現在の作業ディレクトリとして設定します。

    cd ~/terraform
    
  3. 次のコマンドを実行します。

    terraform init
    
  4. 作業ディレクトリが初期化されます。次の出力が表示されます。

    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.

Terraform 構成に基づく実行プランをプレビューする

Terraform 実行プランでは、Terraform が Vertex AI インフラストラクチャとサービスに対して行う計画の変更点が示されます。

次のコマンドを実行して、Terraform の実行プランを表示します。

terraform plan

Cloud Shell を承認するよう求められたら、[承認] をクリックします。認証の際、Cloud Shell はデフォルトでユーザーの認証情報を使用します。

出力は次のようになります。

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # google_notebooks_instance.basic_instance will be created
  + resource "google_notebooks_instance" "basic_instance" {
      + create_time     = (known after apply)
      + id              = (known after apply)
      + labels          = (known after apply)
      + location        = "us-west2-b"
      + machine_type    = "e2-medium"
      + name            = "notebooks-instance-basic"
      + network         = (known after apply)
      + project         = "my_project"
      + proxy_uri       = (known after apply)
      + service_account = (known after apply)
      + state           = (known after apply)
      + subnet          = (known after apply)
      + update_time     = (known after apply)

      + shielded_instance_config {
          + enable_integrity_monitoring = (known after apply)
          + enable_secure_boot          = (known after apply)
          + enable_vtpm                 = (known after apply)
        }

      + vm_image {
          + image_family = "tf-ent-2-9-cu113-notebooks"
          + project      = "deeplearning-platform-release"
        }
    }

  # google_project_service.notebooks will be created
  + resource "google_project_service" "notebooks" {
      + disable_on_destroy = false
      + id                 = (known after apply)
      + project            = (known after apply)
      + service            = "notebooks.googleapis.com"
    }

Plan: 2 to add, 0 to change, 0 to destroy.
  • my_project は、指定した Google Cloud プロジェクト ID を表します。

  • us-west2-b は、指定したユーザー管理ノートブック インスタンスのリージョンとゾーンを表します。

提案された変更を実行プランに適用する

  1. 次のコマンドを実行して、実行プランからの変更点を Vertex AI インフラストラクチャに適用し、ユーザー管理ノートブック インスタンスを作成します。

    terraform apply
    
  2. 出力は次のようになります。

    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
    symbols:
      + create
    
    Terraform will perform the following actions:
    
      # google_notebooks_instance.basic_instance will be created
      + resource "google_notebooks_instance" "basic_instance" {
          + create_time     = (known after apply)
          + id              = (known after apply)
          + labels          = (known after apply)
          + location        = "us-west2-b"
          + machine_type    = "e2-medium"
          + name            = "notebooks-instance-basic"
          + network         = (known after apply)
          + project         = "my_project"
          + proxy_uri       = (known after apply)
          + service_account = (known after apply)
          + state           = (known after apply)
          + subnet          = (known after apply)
          + update_time     = (known after apply)
    
          + shielded_instance_config {
              + enable_integrity_monitoring = (known after apply)
              + enable_secure_boot          = (known after apply)
              + enable_vtpm                 = (known after apply)
            }
    
          + vm_image {
              + image_family = "tf-ent-2-9-cu113-notebooks"
              + project      = "deeplearning-platform-release"
            }
        }
    
      # google_project_service.notebooks will be created
      + resource "google_project_service" "notebooks" {
          + disable_on_destroy = false
          + id                 = (known after apply)
          + project            = (known after apply)
          + service            = "notebooks.googleapis.com"
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    
    • my_project は、指定した Google Cloud プロジェクト ID を表します。

    • us-west2-b は、指定したユーザー管理ノートブック インスタンスのリージョンとゾーンを表します。

  3. yes」と入力して Enter キーを押します。出力は次のようになります。

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    

ユーザー管理ノートブック インスタンスを表示する

Google Cloud コンソールで、[ユーザー管理のノートブック] ページに移動します。

[ユーザー管理のノートブック] に移動

表に「notebooks-instance-basic」という名前の新しいノートブックが表示されます。terraform apply を実行してから新しいユーザー管理ノートブック インスタンスがプロビジョニングされるまで数分かかることがあります。

プロジェクトをクリーンアップする

このチュートリアルで作成した Google Cloud リソースをクリーンアップします。リソースの一部から予期しない料金が発生しないようにするには、次の手順を行います。

  1. Cloud Shell ターミナルで次のコマンドを実行して、terraform フォルダを現在の作業ディレクトリとして設定します。

    cd ~/terraform
    
  2. Terraform 構成に基づいて作成した Vertex AI リソースを削除するには、次のコマンドを実行します。

    terraform destroy
    
  3. 出力は次のようになります。

    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # google_notebooks_instance.basic_instance will be destroyed
      - resource "google_notebooks_instance" "basic_instance" {
          - create_time            = "2022-12-01T21:14:05.065893475Z" -> null
          - id                     = "projects/my_project/locations/us-west2-b/instances/notebooks-instance-basic" -> null
          - install_gpu_driver     = false -> null
          - labels                 = {
              - "goog-caip-notebook" = ""
            } -> null
          - location               = "us-west2-b" -> null
          - machine_type           = "e2-medium" -> null
          - name                   = "notebooks-instance-basic" -> null
          - network                = "https://www.googleapis.com/compute/v1/projects/my_project/global/networks/default" -> null
          - no_proxy_access        = false -> null
          - no_public_ip           = false -> null
          - project                = "my_project" -> null
          - service_account        = "329223940713-compute@developer.gserviceaccount.com" -> null
          - service_account_scopes = [] -> null
          - state                  = "PROVISIONING" -> null
          - subnet                 = "https://www.googleapis.com/compute/v1/projects/my_project/regions/us-west2/subnetworks/default" -> null
          - tags                   = [] -> null
          - update_time            = "2022-12-01T21:14:19.048432376Z" -> null
    
          - shielded_instance_config {
              - enable_integrity_monitoring = true -> null
              - enable_secure_boot          = false -> null
              - enable_vtpm                 = true -> null
            }
    
          - vm_image {
              - image_family = "tf-ent-2-9-cu113-notebooks" -> null
              - project      = "deeplearning-platform-release" -> null
            }
        }
    
      # google_project_service.notebooks will be destroyed
      - resource "google_project_service" "notebooks" {
          - disable_on_destroy = false -> null
          - id                 = "my_project/notebooks.googleapis.com" -> null
          - project            = "my_project" -> null
          - service            = "notebooks.googleapis.com" -> null
        }
    
    Plan: 0 to add, 0 to change, 2 to destroy.
    
    Do you really want to destroy all resources?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.
    
      Enter a value:
    
    • my_project は、指定した Google Cloud プロジェクト ID を表します。

    • us-west2-b は、指定したユーザー管理ノートブック インスタンスのリージョンとゾーンを表します。

  4. yes」と入力して Enter キーを押します。出力は次のようになります。

    Destroy complete! Resources: 2 destroyed.
    
  5. ユーザー管理ノートブック インスタンスが削除されたことを確認するには、[ユーザー管理のノートブック] ページに移動します。

    [ユーザー管理のノートブック] に移動

  6. Cloud Shell エディタを起動する

  7. terraform フォルダとそのコンテンツを削除するには、[Explorer] ペインで terraform フォルダを右クリックし、[Delete] をクリックします。

  8. 確認画面が表示されたら、[OK] をクリックします。

Vertex AI で Terraform を使用する他の方法

このチュートリアルでは、1 つの構成ファイルを作成し、1 つの Terraform リソースに基づいてインフラストラクチャをプロビジョニングして Terraform を使用する方法について説明しました。Terraform は次の方法でも使用できます。

  • 複数の Terraform リソースを同じ Terraform 構成ファイルに追加する。Vertex AI の Terraform リソースのリストについては、Vertex AI で使用可能な Terraform リソースをご覧ください。

  • 複数のフォルダと Terraform 構成ファイルからなるディレクトリ構造を作成する。たとえば、Terraform リソースのタイプごとに別々のフォルダと Terraform 構成ファイルを作成できます。

次のステップ