forked from ntimo/coder-hetzner-cloud-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
191 lines (169 loc) · 4.7 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.23.0"
}
hcloud = {
source = "hetznercloud/hcloud"
version = "1.47.0"
}
}
}
provider "hcloud" {
token = var.hcloud_token
}
provider "coder" {
}
variable "hcloud_token" {
description = <<EOF
Coder requires a Hetzner Cloud token to provision workspaces.
EOF
sensitive = true
validation {
condition = length(var.hcloud_token) == 64
error_message = "Please provide a valid Hetzner Cloud API token."
}
}
variable "instance_location" {
description = "What region should your workspace live in?"
default = "fsn1"
validation {
condition = contains(["nbg1", "fsn1", "hel1"], var.instance_location)
error_message = "Invalid zone!"
}
}
variable "instance_type" {
description = "What instance type should your workspace use?"
default = "cpx11"
validation {
condition = contains(["cx22", "cpx11", "cx32", "cpx21", "cpx31", "cx42", "cx41", "cpx51"], var.instance_type)
error_message = "Invalid instance type!"
}
}
variable "instance_os" {
description = "Which operating system should your workspace use?"
default = "ubuntu-24.04"
validation {
condition = contains([
"ubuntu-24.04",
"fedora-40",
"debian-12",
"centos-stream-9",
"almalinux-9",
"rockylinux-9"
], var.instance_os)
error_message = "Invalid OS!"
}
}
variable "volume_size" {
description = "How much storage space do you need in GB (can't be less then 10)?"
default = "10"
validation {
condition = var.volume_size >= 10
error_message = "Invalid volume size!"
}
}
variable "code_server" {
description = "Should Code Server be installed?"
default = "true"
validation {
condition = contains(["true","false"], var.code_server)
error_message = "Your answer can only be yes or no!"
}
}
data "coder_workspace" "me" {}
resource "coder_agent" "dev" {
arch = "amd64"
os = "linux"
metadata {
display_name = "CPU Usage"
key = "cpu_usage"
order = 0
script = "coder stat cpu"
interval = 10
timeout = 1
}
metadata {
display_name = "RAM Usage"
key = "ram_usage"
order = 1
script = "coder stat mem"
interval = 10
timeout = 1
}
metadata {
display_name = "Disk Usage (Host)"
key = "disk_host"
order = 6
script = "coder stat disk --path /"
interval = 600
timeout = 10
}
}
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
slug = "code-server"
display_name = "VS Code"
icon = "${data.coder_workspace.me.access_url}/icon/code.svg"
url = "http://localhost:8080"
share = "owner"
subdomain = false
healthcheck {
url = "http://localhost:8080/healtz"
interval = 5
threshold = 6
}
}
# Generate a dummy ssh key that is not accessible so Hetzner cloud does not spam the admin with emails.
resource "tls_private_key" "rsa_4096" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "hcloud_ssh_key" "root" {
name = "coder-${data.coder_workspace.me.name}-root"
public_key = tls_private_key.rsa_4096.public_key_openssh
}
resource "hcloud_server" "root" {
count = data.coder_workspace.me.start_count
name = "coder-${data.coder_workspace.me.name}-root"
server_type = var.instance_type
location = var.instance_location
image = var.instance_os
ssh_keys = [hcloud_ssh_key.root.id]
user_data = templatefile("cloud-config.yaml.tftpl", {
username = data.coder_workspace.me.name
volume_path = "/dev/disk/by-id/scsi-0HC_Volume_${hcloud_volume.root.id}"
init_script = base64encode(coder_agent.dev.init_script)
coder_agent_token = coder_agent.dev.token
code_server_setup = var.code_server
})
}
resource "hcloud_volume" "root" {
name = "coder-${data.coder_workspace.me.name}-root"
size = var.volume_size
format = "ext4"
location = var.instance_location
}
resource "hcloud_volume_attachment" "root" {
count = data.coder_workspace.me.start_count
volume_id = hcloud_volume.root.id
server_id = hcloud_server.root[count.index].id
automount = false
}
resource "hcloud_firewall" "root" {
name = "coder-${data.coder_workspace.me.name}-root"
rule {
direction = "in"
protocol = "icmp"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
resource "hcloud_firewall_attachment" "root_fw_attach" {
count = data.coder_workspace.me.start_count
firewall_id = hcloud_firewall.root.id
server_ids = [hcloud_server.root[count.index].id]
}