Terraform을 사용하여 사용자 관리형 노트북 인스턴스 만들기

이 튜토리얼에서는 Terraform 구성 파일을 만들고 서비스 계정 모드에서 Vertex AI에 사용자 관리 노트북 인스턴스를 프로비저닝하는 방법을 설명합니다.

다음 도구를 사용하여 이 튜토리얼의 단계를 완료합니다.

  • Terraform CLI: Terraform은 Cloud Shell에 사전 설치되어 있습니다. Terraform CLI 명령어를 사용하기 위해 Terraform을 별개로 설치할 필요가 없습니다.

  • Cloud Shell 편집기: Cloud Shell 편집기를 사용하여 Terraform 구성 파일을 생성 및 관리하고 폴더 구조를 관리합니다.

  • Cloud Shell 터미널 Cloud Shell 터미널을 사용하여 terraform init, terraform plan, terraform apply, terraform destroy 등의 Terraform CLI 명령어를 실행합니다.

시작하기 전에

Terraform을 사용하여 Vertex AI 리소스에 대해 인프라를 빌드하려면 먼저 Google Cloud 프로젝트 및 개발 환경을 설정합니다. 이 섹션에서는 또한 Terraform이 프로젝트 리소스와 상호작용하기 위해 사용하는 Vertex AI API를 사용 설정하는 방법을 설명합니다.

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  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 역할을 사용자 계정에 부여해 달라고 요청하세요.

  • Vertex AI Workbench에서 인스턴스를 만들고 삭제하기 위한 노트북 관리자(roles/notebooks.admin)
  • 서비스 계정으로 작업을 실행하기 위한 서비스 계정 사용자(roles/iam.serviceAccountUser) 이 튜토리얼에서는 서비스 계정을 지정하지 않으므로 사용자 관리 노트북 인스턴스가 기본 Compute Engine 서비스 계정을 사용합니다.
  • 서비스 상태 및 작업을 검사하고 프로젝트 리소스의 할당량과 결제를 사용하는 서비스 사용량 소비자(roles/serviceusage.serviceUsageConsumer)

역할 부여에 대한 자세한 내용은 액세스 관리를 참조하세요.

관리자는 커스텀 역할이나 다른 사전 정의된 역할을 통해 사용자 계정에 필요한 권한을 부여할 수도 있습니다.

폴더 구조 및 Terraform 구성 파일 만들기

  1. Google Cloud 콘솔에서 Cloud Shell을 활성화합니다.

    Cloud Shell 활성화

    Google Cloud 콘솔 하단에서 Cloud Shell 세션이 시작되고 명령줄 프롬프트가 표시됩니다. Cloud Shell은 Google Cloud CLI가 사전 설치된 셸 환경으로, 현재 프로젝트의 값이 이미 설정되어 있습니다. 세션이 초기화되는 데 몇 초 정도 걸릴 수 있습니다.

  1. Cloud Shell 터미널에서 다음 명령어를 실행하여 홈 디렉터리를 활성 디렉터리로 설정하세요.
    cd
  2. 다음 명령어를 실행하여 terraform이라는 새 폴더를 만듭니다.
    mkdir terraform
  3. Cloud Shell 편집기를 실행합니다.
  4. 탐색기 창에서 terraform 폴더를 마우스 오른쪽 버튼으로 클릭한 후 새 파일을 클릭합니다.
  5. 파일 이름으로 main.tf를 입력하고 확인을 클릭합니다.

Terraform 구성 파일에서 인프라 정의

  1. Cloud Shell 편집기에서 main.tf 파일을 엽니다.

  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이 작업 디렉터리를 초기화합니다. 다음과 같은 출력이 표시됩니다.

    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 폴더와 폴더 안의 콘텐츠를 삭제하려면 탐색기 창에서 terraform 폴더를 마우스 오른쪽 버튼으로 클릭한 다음 삭제를 클릭합니다.

  8. 메시지가 나타나면 확인을 클릭합니다.

Vertex AI에서 Terraform을 사용하는 기타 방법

이 튜토리얼에서는 하나의 Terraform 리소스를 기반으로 하나의 구성 파일만 만들고 인프라를 프로비저닝하여 Terraform을 사용하는 방법을 보여줍니다. 다음과 같은 방법으로 Terraform을 사용할 수도 있습니다.

  • 동일한 Terraform 구성 파일에 여러 Terraform 리소스를 추가합니다. Vertex AI의 Terraform 리소스 목록은 Vertex AI에 사용 가능한 Terraform 리소스를 참조하세요.

  • 여러 폴더와 Terraform 구성 파일로 구성되는 디렉터리 구조를 만듭니다. 예를 들어 각 유형의 Terraform 리소스에 대해 별개의 폴더와 Terraform 구성 파일을 만들 수 있습니다.

다음 단계