TemplatesModules
Back to Templates
Google Compute Engine (VM Container) Icon

Google Compute Engine (VM Container)

By:
Provision Google Compute Engine instances as Coder workspaces
Source
README
Resources (1)
Variables (1)

Copy and paste the following into main.tf and run coder template push:

1terraform {
2  required_providers {
3    coder = {
4      source = "coder/coder"
5    }
6    google = {
7      source = "hashicorp/google"
8    }
9  }
10}
11
12provider "coder" {
13}
14
15variable "project_id" {
16  description = "Which Google Compute Project should your workspace live in?"
17}
18
19data "coder_parameter" "zone" {
20  name         = "zone"
21  display_name = "Zone"
22  description  = "Which zone should your workspace live in?"
23  type         = "string"
24  default      = "us-central1-a"
25  icon         = "/emojis/1f30e.png"
26  mutable      = false
27  option {
28    name  = "North America (Northeast)"
29    value = "northamerica-northeast1-a"
30    icon  = "/emojis/1f1fa-1f1f8.png"
31  }
32  option {
33    name  = "North America (Central)"
34    value = "us-central1-a"
35    icon  = "/emojis/1f1fa-1f1f8.png"
36  }
37  option {
38    name  = "North America (West)"
39    value = "us-west2-c"
40    icon  = "/emojis/1f1fa-1f1f8.png"
41  }
42  option {
43    name  = "Europe (West)"
44    value = "europe-west4-b"
45    icon  = "/emojis/1f1ea-1f1fa.png"
46  }
47  option {
48    name  = "South America (East)"
49    value = "southamerica-east1-a"
50    icon  = "/emojis/1f1e7-1f1f7.png"
51  }
52}
53
54provider "google" {
55  zone    = data.coder_parameter.zone.value
56  project = var.project_id
57}
58
59data "google_compute_default_service_account" "default" {
60}
61
62data "coder_workspace" "me" {
63}
64
65resource "coder_agent" "main" {
66  auth           = "google-instance-identity"
67  arch           = "amd64"
68  os             = "linux"
69  startup_script = <<-EOT
70    set -e
71
72    # install and start code-server
73    curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
74    /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
75  EOT
76}
77
78# code-server
79resource "coder_app" "code-server" {
80  agent_id     = coder_agent.main.id
81  slug         = "code-server"
82  display_name = "code-server"
83  icon         = "/icon/code.svg"
84  url          = "http://localhost:13337?folder=/home/coder"
85  subdomain    = false
86  share        = "owner"
87
88  healthcheck {
89    url       = "http://localhost:13337/healthz"
90    interval  = 3
91    threshold = 10
92  }
93}
94
95module "gce-container" {
96  source  = "terraform-google-modules/container-vm/google"
97  version = "3.0.0"
98
99  container = {
100    image   = "codercom/enterprise-base:ubuntu"
101    command = ["sh"]
102    args    = ["-c", coder_agent.main.init_script]
103    securityContext = {
104      privileged : true
105    }
106  }
107}
108
109resource "google_compute_instance" "dev" {
110  zone         = data.coder_parameter.zone.value
111  count        = data.coder_workspace.me.start_count
112  name         = "coder-${lower(data.coder_workspace.me.owner)}-${lower(data.coder_workspace.me.name)}"
113  machine_type = "e2-medium"
114  network_interface {
115    network = "default"
116    access_config {
117      // Ephemeral public IP
118    }
119  }
120  boot_disk {
121    initialize_params {
122      image = module.gce-container.source_image
123    }
124  }
125  service_account {
126    email  = data.google_compute_default_service_account.default.email
127    scopes = ["cloud-platform"]
128  }
129  metadata = {
130    "gce-container-declaration" = module.gce-container.metadata_value
131  }
132  labels = {
133    container-vm = module.gce-container.vm_container_label
134  }
135}
136
137resource "coder_agent_instance" "dev" {
138  count       = data.coder_workspace.me.start_count
139  agent_id    = coder_agent.main.id
140  instance_id = google_compute_instance.dev[0].instance_id
141}
142
143resource "coder_metadata" "workspace_info" {
144  count       = data.coder_workspace.me.start_count
145  resource_id = google_compute_instance.dev[0].id
146
147  item {
148    key   = "image"
149    value = module.gce-container.container.image
150  }
151}
152