Colab EnterpriseのランタイムテンプレートをTerraformで作る方法について
今回はVertex AIのColab EnterpriseのランタイムテンプレートをTerraformで記述してみました。以前コンソールからランタイムテンプレートを作ったことはありましたが、今回はその設定をTerraformで再現してみようと思います。以前の該当記事は以下になるので、ぜひ合わせてご覧ください。
Google CloudでTerraformを試してみた系は以下のスクラップにまとめていますので合わせてご覧ください!
ランタイムテンプレートの設定について
先ほど添付した記事の内容から、今回設定する仕様をまとめてみます。
- ランタイムの基本情報
- テンプレート名:
sample-template
- リージョン:
asia-northeast1
- テンプレート名:
- コンピューティングの構成
- マシンタイプ:
n2-standard-4
- 予約:使用しない
- データディスクの種類:標準ディスク(pd-standard)
- データディスクサイズ:100
- 暗号化:Googleが管理する暗号鍵
- アイドル状態でのシャットダウン
- アイドル状態でのシャットダウンを有効にする
- シャットダウンまでの非アクティブ時間(分):10
- マシンタイプ:
- 環境
- Latest(currently Python 3.11)
- ネットワークとセキュリティ(ネットワークは今回新規作成します)
- ネットワーク:
colab-test-default
- サブネットワーク:
colab-test-default
- 公共のインターネットアクセスを有効にする
- エンドユーザー認証情報を有効にする
- ネットワーク:
それでは早速実装開始
今回利用するリソース定義は以下から確認できます。
変数の定義
まずはGoogle CloudのプロジェクトIDやリージョンを設定するためにvariables.tf
を以下のように作成しました。設定については上記のリストに沿って設定しています。アイドルタイムのシャットダウン時間については秒で指定する必要があるようで、600秒(10分)で指定しています。
variable "project_id" {
description = "The Google Cloud project ID"
type = string
}
variable "region" {
description = "The Google Cloud region"
type = string
default = "asia-northeast1"
}
# Colab Enterprise variables
variable "colab_enterprise_display_name" {
description = "Colab Enterprise display name"
type = string
default = "sample-template"
}
variable "colab_enterprise_machine_type" {
description = "Colab Enterprise machine type"
type = string
default = "n2-standard-4"
}
variable "data_persistent_disk_spec" {
description = "Data persistent disk spec"
type = object({
disk_type = string
disk_size_gb = number
})
default = {
disk_type = "pd-standard"
disk_size_gb = 100
}
}
variable "vpc_network" {
description = "VPC network"
type = object({
network = string
subnetwork = string
})
default = {
network = "default"
subnetwork = "default-asia-northeast1"
}
}
variable "idle_timeout" {
description = "Idle timeout"
type = string
default = "600s"
}
リソース定義
それでは次にmain.tf
にリソースの定義をします。基本的に仕様リストに従って実装しています。Pythonの環境についてはしていなければ最新のものに指定されるため、今回は明示的に指定していません。
provider "google" {
project = var.project_id
region = var.region
}
resource "google_compute_network" "my_network" {
name = "colab-test-default"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "my_subnetwork" {
name = "colab-test-default"
network = google_compute_network.my_network.id
region = var.region
ip_cidr_range = "10.0.1.0/24"
}
resource "google_colab_runtime_template" "runtime-template" {
name = "colab-runtime-template"
display_name = var.colab_enterprise_display_name
location = var.region
machine_spec {
machine_type = var.colab_enterprise_machine_type
}
data_persistent_disk_spec {
disk_type = var.data_persistent_disk_spec.disk_type
disk_size_gb = var.data_persistent_disk_spec.disk_size_gb
}
network_spec {
enable_internet_access = true
network = google_compute_network.my_network.id
subnetwork = google_compute_subnetwork.my_subnetwork.id
}
idle_shutdown_config {
idle_timeout = var.idle_timeout
}
euc_config {
euc_disabled = false
}
}
それではterraform plan
を実行して作成されるリソースのプランをみてみます。内容を見ると、まずVPCネットワークを作成したのち、そのネットワークを利用したテンプレートの定義になっていることが確認できます。
terraform plan
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_colab_runtime_template.runtime-template will be created
+ resource "google_colab_runtime_template" "runtime-template" {
+ display_name = "sample-template"
+ effective_labels = {
+ "goog-terraform-provisioned" = "true"
}
+ id = (known after apply)
+ labels = (known after apply)
+ location = "asia-northeast1"
+ name = "colab-runtime-template"
+ project = "project_id"
+ terraform_labels = {
+ "goog-terraform-provisioned" = "true"
}
+ data_persistent_disk_spec {
+ disk_size_gb = "100"
+ disk_type = "pd-standard"
}
+ euc_config {
+ euc_disabled = false
}
+ idle_shutdown_config {
+ idle_timeout = "600s"
}
+ machine_spec {
+ accelerator_count = (known after apply)
+ machine_type = "n2-standard-4"
}
+ network_spec {
+ enable_internet_access = true
+ network = (known after apply)
+ subnetwork = (known after apply)
}
+ shielded_vm_config (known after apply)
+ software_config (known after apply)
}
# google_compute_network.my_network will be created
+ resource "google_compute_network" "my_network" {
+ auto_create_subnetworks = false
+ bgp_always_compare_med = (known after apply)
+ bgp_best_path_selection_mode = (known after apply)
+ bgp_inter_region_cost = (known after apply)
+ delete_default_routes_on_create = false
+ gateway_ipv4 = (known after apply)
+ id = (known after apply)
+ internal_ipv6_range = (known after apply)
+ mtu = (known after apply)
+ name = "colab-test-default"
+ network_firewall_policy_enforcement_order = "AFTER_CLASSIC_FIREWALL"
+ network_id = (known after apply)
+ numeric_id = (known after apply)
+ project = "project_id"
+ routing_mode = (known after apply)
+ self_link = (known after apply)
}
# google_compute_subnetwork.my_subnetwork will be created
+ resource "google_compute_subnetwork" "my_subnetwork" {
+ creation_timestamp = (known after apply)
+ external_ipv6_prefix = (known after apply)
+ fingerprint = (known after apply)
+ gateway_address = (known after apply)
+ id = (known after apply)
+ internal_ipv6_prefix = (known after apply)
+ ip_cidr_range = "10.0.1.0/24"
+ ipv6_cidr_range = (known after apply)
+ ipv6_gce_endpoint = (known after apply)
+ name = "colab-test-default"
+ network = (known after apply)
+ private_ip_google_access = (known after apply)
+ private_ipv6_google_access = (known after apply)
+ project = "project_id"
+ purpose = (known after apply)
+ region = "asia-northeast1"
+ self_link = (known after apply)
+ stack_type = (known after apply)
+ state = (known after apply)
+ subnetwork_id = (known after apply)
+ secondary_ip_range (known after apply)
}
Plan: 3 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
リソースを作成する
それではterraform apply
を実行してリソースを作成します。前回作成した結果と今回作成した結果を並べてみます。結果を比較すると、どちらも構成が同じに作成できていることが確認できました。
手動作成したテンプレート | Terraformで作成したテンプレート |
---|---|
![]() |
![]() |
最後に今回作成したテンプレートをterraform destroy
で削除します。
まとめ
今回はTerraformを利用してColab Enterpriseのランタイムテンプレートを作成してみました。マニュアルでもそこまで手間はかからないものの、IaCとしてインフラを管理するのがベストだと思うので、Colab Enterpriseを利用予定の方はぜひ参考にしていただければと思います。
Discussion