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