infrastructure-ng/id.tjo.space/terraform/main.tf

82 lines
1.8 KiB
Terraform
Raw Normal View History

2025-03-05 21:43:20 +00:00
resource "hcloud_ssh_key" "main" {
2025-03-06 20:11:57 +00:00
for_each = var.ssh_keys
2025-03-06 19:04:18 +00:00
2025-03-05 21:43:20 +00:00
name = each.key
2025-03-06 19:04:18 +00:00
public_key = each.value
2025-03-05 21:43:20 +00:00
}
2025-03-06 20:11:57 +00:00
locals {
nodes = {
for k in var.nodes : k => {
meta = {
name = k
domain = "next.id.tjo.space"
}
}
}
}
2025-03-05 21:43:20 +00:00
resource "hcloud_server" "main" {
2025-03-06 20:11:57 +00:00
for_each = local.nodes
2025-03-06 19:04:18 +00:00
2025-03-06 20:11:57 +00:00
name = "${each.value.meta.name}.${each.value.meta.domain}"
2025-03-06 19:04:18 +00:00
2025-03-05 21:43:20 +00:00
image = "ubuntu-24.04"
server_type = "cax11"
2025-03-06 20:11:57 +00:00
datacenter = "hel1-dc2"
2025-03-05 21:43:20 +00:00
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
2025-03-06 20:11:57 +00:00
backups = true
2025-03-06 19:04:18 +00:00
ssh_keys = [for key, value in var.ssh_keys : hcloud_ssh_key.main[key].id]
2025-03-05 21:43:20 +00:00
user_data = <<-EOF
#cloud-config
2025-03-06 20:11:57 +00:00
hostname: "${each.value.meta.name}"
fqdn: "${each.value.meta.name}.${each.value.meta.domain}"
2025-03-05 21:43:20 +00:00
prefer_fqdn_over_hostname: true
2025-03-06 20:11:57 +00:00
write_files:
- path: /etc/tjo.space/meta.json
encoding: base64
content: ${base64encode(jsonencode(each.value.meta))}
2025-03-07 11:13:53 +00:00
- path: /tmp/provision.sh
encoding: base64
content: ${base64encode(file("${path.module}/../provision.sh"))}
2025-03-05 21:43:20 +00:00
packages:
- git
2025-03-05 21:49:55 +00:00
- curl
2025-03-05 21:43:20 +00:00
package_update: true
package_upgrade: true
power_state:
mode: reboot
swap:
filename: /swapfile
size: 512M
runcmd:
2025-03-07 11:13:53 +00:00
- "chmod +x /tmp/provision.sh"
- "/tmp/provision.sh"
- "rm /tmp/provision.sh"
2025-03-06 19:31:24 +00:00
EOF
2025-03-05 21:43:20 +00:00
}
resource "dnsimple_zone_record" "a" {
2025-03-06 20:11:57 +00:00
for_each = local.nodes
2025-03-06 19:04:18 +00:00
2025-03-05 21:43:20 +00:00
zone_name = "tjo.space"
2025-03-06 20:11:57 +00:00
name = trimsuffix(each.value.meta.domain, ".tjo.space")
2025-03-06 19:04:18 +00:00
value = hcloud_server.main[each.key].ipv4_address
2025-03-05 21:43:20 +00:00
type = "A"
ttl = 300
}
2025-03-07 11:13:53 +00:00
resource "dnsimple_zone_record" "aaaa" {
for_each = local.nodes
zone_name = "tjo.space"
name = trimsuffix(each.value.meta.domain, ".tjo.space")
value = hcloud_server.main[each.key].ipv6_address
type = "AAAA"
ttl = 300
}