-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
151 lines (133 loc) · 4.04 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
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.5.0"
}
hcloud = {
source = "hetznercloud/hcloud"
version = "1.33.2"
}
}
}
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 = "nbg1"
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 = "cx11"
validation {
condition = contains(["cx11", "cx21", "cx31", "cx41", "cx51"], var.instance_type)
error_message = "Invalid instance type!"
}
}
variable "instance_os" {
description = "Which operating system should your workspace use?"
default = "ubuntu-20.04"
validation {
condition = contains(["ubuntu-22.04","ubuntu-20.04", "ubuntu-18.04", "debian-11", "debian-10"], 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"
}
resource "coder_app" "code-server" {
count = var.code_server ? 1 : 0
agent_id = coder_agent.dev.id
name = "code-server"
icon = "/icon/code.svg"
url = "http://localhost:8080"
relative_path = true
}
# 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.owner}-${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.owner}-${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.owner
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.owner}-${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.owner}-${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]
}