Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save misskecupbung/d684d932d5b4efba59ce817889a10eb6 to your computer and use it in GitHub Desktop.

Select an option

Save misskecupbung/d684d932d5b4efba59ce817889a10eb6 to your computer and use it in GitHub Desktop.

Automating AI Infrastructure on GCP with Infrastructure as Code

  1. Open Cloud Shell
  2. Enable the required APIs
gcloud services enable compute.googleapis.com \
                         iap.googleapis.com \
                         aiplatform.googleapis.com \
                         notebooks.googleapis.com \
                         cloudkms.googleapis.com \
                         cloudresourcemanager.googleapis.com \
                         iam.googleapis.com
  1. Initiate the folders
mkdir tf-vertex-ai-[name]
cd tf-vertex-ai-[name]
  1. Create the prerequisite files
touch main.tf network.tf outputs.tf variables.tf
  1. Edit main.tf
resource "google_service_account" "workbench_sa" {
  account_id   = "vertex-ai-workbench-sa-[name]"
  display_name = "Vertex AI Workbench Service Account"
  project      = var.project_id
}

module "simple_vertex_ai_workbench" {
  source  = "GoogleCloudPlatform/vertex-ai/google//modules/workbench"
  version = "~> 0.2"

  name       = "simple-vertex-ai-workbench-[name]"
  location   = "us-central1-a"
  project_id = var.project_id

  machine_type = "e2-standard-2"

  labels = {
    env  = "test"
    type = "workbench"
  }

  service_accounts = [
    {
      email = google_service_account.workbench_sa.email
    },
  ]

  data_disks = [
    {
      disk_size_gb = 20
      disk_type    = "PD_BALANCED"
    },
  ]

  network_interfaces = [
    {
      network  = module.test-vpc-module.network_id
      subnet   = module.test-vpc-module.subnets_ids[0]
      nic_type = "GVNIC"
    }
  ]
}
  1. Edit network.tf
resource "random_id" "suffix" {
  byte_length = 4
}

module "test-vpc-module" {
  source       = "terraform-google-modules/network/google"
  version      = "~> 9.0"
  project_id   = var.project_id
  network_name = "simple-workbench-[name]"
  mtu          = 1460
  subnets = [
    {
      subnet_name           = "simple-subnet-01-[name]"
      subnet_ip             = "10.10.1X.0/24"
      subnet_region         = "us-central1"
      subnet_private_access = true
    },
  ]
}
  1. Edit variables.tf
variable "project_id" {
  description = "The ID of the project in which the resource belongs"
  type        = string
}
  1. Edit outputs.tf
output "project_id" {
  value       = var.project_id
  description = "The project ID"
}

output "workbench_name" {
  value       = module.simple_vertex_ai_workbench.workbench_name
  description = "The name of the Vertex AI Workbench instance"
}

output "workbench" {
  value       = module.simple_vertex_ai_workbench
  description = "The Vertex AI Workbench instance"
}

output "service_account" {
  value       = google_service_account.workbench_sa.email
  description = "The service account for Vertex AI Workbench instance"
}

output "network" {
  value       = module.test-vpc-module.network_self_link
  description = "The network for Vertex AI Workbench instance"
}

output "subnet" {
  value       = module.test-vpc-module.subnets_self_links[0]
  description = "The subnet for Vertex AI Workbench instance"
}
  1. Export the project_id
export TF_VAR_project_id="your_project_id"
  1. Init the terraform project
terraform init
  1. Plan the Terraform project
terraform plan
  1. Apply the Terraform project
terraform apply
  1. Destroy the Terraform project
terraform destroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment