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 icon = "/emojis/1f30e.png"
25 default = "us-central1-a"
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}
64data "coder_workspace_owner" "me" {}
65
66resource "google_compute_disk" "root" {
67 name = "coder-${data.coder_workspace.me.id}-root"
68 type = "pd-ssd"
69 zone = data.coder_parameter.zone.value
70 image = "debian-cloud/debian-11"
71 lifecycle {
72 ignore_changes = [name, image]
73 }
74}
75
76resource "coder_agent" "main" {
77 auth = "google-instance-identity"
78 arch = "amd64"
79 os = "linux"
80 startup_script = <<-EOT
81 set -e
82
83 # install and start code-server
84 curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
85 /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
86 EOT
87
88 metadata {
89 key = "cpu"
90 display_name = "CPU Usage"
91 interval = 5
92 timeout = 5
93 script = <<-EOT
94 #!/bin/bash
95 set -e
96 top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
97 EOT
98 }
99 metadata {
100 key = "memory"
101 display_name = "Memory Usage"
102 interval = 5
103 timeout = 5
104 script = <<-EOT
105 #!/bin/bash
106 set -e
107 free -m | awk 'NR==2{printf "%.2f%%\t", $3*100/$2 }'
108 EOT
109 }
110 metadata {
111 key = "disk"
112 display_name = "Disk Usage"
113 interval = 600 # every 10 minutes
114 timeout = 30 # df can take a while on large filesystems
115 script = <<-EOT
116 #!/bin/bash
117 set -e
118 df /home/coder | awk '$NF=="/"{printf "%s", $5}'
119 EOT
120 }
121}
122
123# code-server
124resource "coder_app" "code-server" {
125 agent_id = coder_agent.main.id
126 slug = "code-server"
127 display_name = "code-server"
128 icon = "/icon/code.svg"
129 url = "http://localhost:13337?folder=/home/coder"
130 subdomain = false
131 share = "owner"
132
133 healthcheck {
134 url = "http://localhost:13337/healthz"
135 interval = 3
136 threshold = 10
137 }
138}
139
140resource "google_compute_instance" "dev" {
141 zone = data.coder_parameter.zone.value
142 count = data.coder_workspace.me.start_count
143 name = "coder-${lower(data.coder_workspace_owner.me.name)}-${lower(data.coder_workspace.me.name)}-root"
144 machine_type = "e2-medium"
145 network_interface {
146 network = "default"
147 access_config {
148 // Ephemeral public IP
149 }
150 }
151 boot_disk {
152 auto_delete = false
153 source = google_compute_disk.root.name
154 }
155 service_account {
156 email = data.google_compute_default_service_account.default.email
157 scopes = ["cloud-platform"]
158 }
159 # The startup script runs as root with no $HOME environment set up, so instead of directly
160 # running the agent init script, create a user (with a homedir, default shell and sudo
161 # permissions) and execute the init script as that user.
162 metadata_startup_script = <<EOMETA
163#!/usr/bin/env sh
164set -eux
165
166# If user does not exist, create it and set up passwordless sudo
167if ! id -u "${local.linux_user}" >/dev/null 2>&1; then
168 useradd -m -s /bin/bash "${local.linux_user}"
169 echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
170fi
171
172exec sudo -u "${local.linux_user}" sh -c '${coder_agent.main.init_script}'
173EOMETA
174}
175
176locals {
177 # Ensure Coder username is a valid Linux username
178 linux_user = lower(substr(data.coder_workspace_owner.me.name, 0, 32))
179}
180
181resource "coder_metadata" "workspace_info" {
182 count = data.coder_workspace.me.start_count
183 resource_id = google_compute_instance.dev[0].id
184
185 item {
186 key = "type"
187 value = google_compute_instance.dev[0].machine_type
188 }
189}
190
191resource "coder_metadata" "home_info" {
192 resource_id = google_compute_disk.root.id
193
194 item {
195 key = "size"
196 value = "${google_compute_disk.root.size} GiB"
197 }
198}
199