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}
64data "coder_workspace_owner" "me" {}
65
66resource "coder_agent" "main" {
67 auth = "google-instance-identity"
68 arch = "amd64"
69 os = "linux"
70 startup_script = <<-EOT
71 set -e
72
73 # install and start code-server
74 curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
75 /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
76 EOT
77}
78
79# code-server
80resource "coder_app" "code-server" {
81 agent_id = coder_agent.main.id
82 slug = "code-server"
83 display_name = "code-server"
84 icon = "/icon/code.svg"
85 url = "http://localhost:13337?folder=/home/coder"
86 subdomain = false
87 share = "owner"
88
89 healthcheck {
90 url = "http://localhost:13337/healthz"
91 interval = 3
92 threshold = 10
93 }
94}
95
96module "gce-container" {
97 source = "terraform-google-modules/container-vm/google"
98 version = "3.0.0"
99
100 container = {
101 image = "codercom/enterprise-base:ubuntu"
102 command = ["sh"]
103 args = ["-c", coder_agent.main.init_script]
104 securityContext = {
105 privileged : true
106 }
107 }
108}
109
110resource "google_compute_instance" "dev" {
111 zone = data.coder_parameter.zone.value
112 count = data.coder_workspace.me.start_count
113 name = "coder-${lower(data.coder_workspace_owner.me.name)}-${lower(data.coder_workspace.me.name)}"
114 machine_type = "e2-medium"
115 network_interface {
116 network = "default"
117 access_config {
118 // Ephemeral public IP
119 }
120 }
121 boot_disk {
122 initialize_params {
123 image = module.gce-container.source_image
124 }
125 }
126 service_account {
127 email = data.google_compute_default_service_account.default.email
128 scopes = ["cloud-platform"]
129 }
130 metadata = {
131 "gce-container-declaration" = module.gce-container.metadata_value
132 }
133 labels = {
134 container-vm = module.gce-container.vm_container_label
135 }
136}
137
138resource "coder_agent_instance" "dev" {
139 count = data.coder_workspace.me.start_count
140 agent_id = coder_agent.main.id
141 instance_id = google_compute_instance.dev[0].instance_id
142}
143
144resource "coder_metadata" "workspace_info" {
145 count = data.coder_workspace.me.start_count
146 resource_id = google_compute_instance.dev[0].id
147
148 item {
149 key = "image"
150 value = module.gce-container.container.image
151 }
152}
153