Skip to content

Use Terraform for Cloud Workstation #2

@NimJay

Description

@NimJay
  • We can use Terraform to create the Cloud Workstation cluster, configuration, and workstation.
  • It's likely that someone using this demo will be unfamiliar with Google Cloud workstations — so we should abstract away the some Workstation-related jargon and instructions.
  • We can then replace some of the instructions under "Part 1 - Create a Cloud Workstations Cluster", inside /README.md.

Example code

Here's some related code (from a different project) that we can reuse/repurpose.

  • The main.tf file:
variable "project_id" {
  type        = string
  description = "The Google Cloud project ID."
}

provider "google" {
  project = var.project_id
}

provider "google-beta" {
  project = var.project_id
}

# Enable Google Cloud APIs.
module "enable_base_google_apis" {
  source                      = "terraform-google-modules/project-factory/google//modules/project_services"
  version                     = "~> 14.0"
  disable_services_on_destroy = false
  activate_apis = [
    "cloudresourcemanager.googleapis.com",
    "compute.googleapis.com",
    "iam.googleapis.com",
    "workstations.googleapis.com",
  ]
  project_id = var.project_id
}

# Create a VPC (Virtual Private Cloud) network.
resource "google_compute_network" "network" {
  provider                = google-beta
  name                    = "duet-ai-demo-network"
  auto_create_subnetworks = true
}

# Create a subnetwork within the network.
resource "google_compute_subnetwork" "subnetwork" {
  provider      = google-beta
  name          = "duet-ai-demo-subnetwork"
  ip_cidr_range = "10.0.0.0/24"
  region        = "us-central1"
  network       = google_compute_network.network.name
}
  • The providers.tf file:
# Google Cloud recommends pinning provider versions to minor.
# See https://cloud.google.com/docs/terraform/best-practices-for-terraform#minor-provider-versions
terraform {
  required_version = ">= 0.13"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.7.0"
    }
    google-beta = {
      source  = "hashicorp/google-beta"
      version = "~> 5.7.0"
    }
  }
}
  • The workstation.tf file:
# Create the cluster for the workstation.
# Workstation clusters can take about 15 minutes to create.
resource "google_workstations_workstation_cluster" "workstation_cluster" {
  provider               = google-beta
  workstation_cluster_id = "duet-ai-demo-workstation-cluster"
  network                = google_compute_network.network.id
  subnetwork             = google_compute_subnetwork.subnetwork.id
  location               = "us-central1"
}

# Create the config for the workstation.
# Workstation configs can take about 1 minute to create.
resource "google_workstations_workstation_config" "workstation_config" {
  provider               = google-beta
  workstation_config_id  = "duet-ai-demo-workstation-config"
  workstation_cluster_id = google_workstations_workstation_cluster.workstation_cluster.workstation_cluster_id
  location               = "us-central1"
  running_timeout        = "3600s"
  host {
    gce_instance {
      machine_type                = "e2-standard-4" # e2-standard-4 has 4 vCPUs, 2 cores, & 16GB RAM.
      boot_disk_size_gb           = 35
      disable_public_ip_addresses = false
    }
  }
  persistent_directories {
    mount_path = "/home"
    gce_pd {
      size_gb        = 200
      fs_type        = "ext4"
      disk_type      = "pd-standard"
      reclaim_policy = "DELETE"
    }
  }
}

# Create the workstation.
resource "google_workstations_workstation" "workstation" {
  provider               = google-beta
  workstation_id         = "duet-ai-demo-workstation"
  workstation_config_id  = google_workstations_workstation_config.workstation_config.workstation_config_id
  workstation_cluster_id = google_workstations_workstation_cluster.workstation_cluster.workstation_cluster_id
  location               = "us-central1"
}
  • Markdown instructions:
1.  Set your project ID as an environment variable:
    ```bash
    export PROJECT_ID=REPLACE_THIS
    ```

1.  Move into the folder that contains the Terraform code that creates the Cloud workstation:
    ```bash
    cd terraform/workstation
    ```

1.  Initialize Terraform and install Terraform provider plugins:
    ```bash
    terraform init
    ```

1.  Check that your Terraform will work and that it is prepared to create the necessary resources:
    ```bash
    terraform plan -var project_id="${PROJECT_ID}"
    ```

1.  Create the Google Cloud Workstation cluster, workstation configuration, and workstation:
    ```bash
    terraform apply -var project_id="${PROJECT_ID}"
    ```
    This can take about 20 minutes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions