infrastructure/hcloud.tf

64 lines
1.8 KiB
HCL

resource "hcloud_ssh_key" "default" {
name = var.default_ssh_key.name
public_key = var.default_ssh_key.public_key
}
data "template_file" "cloud_init" {
template = "${file("./tf-templates/cloud-init.yaml.tpl")}"
vars = {
tailscale_authkey = "${tailscale_tailnet_key.cloud_init_key.key}"
default_ssh_key = var.default_ssh_key.public_key
}
}
locals {
hetzner_hosts = {for key, val in var.hosts : key => val if val.provider == "hetzner"}
}
resource "hcloud_primary_ip" "node_ipv4_addresses" {
for_each = local.hetzner_hosts
name = "primary_ipv4_${each.value.hostname}"
datacenter = each.value.datacenter
type = "ipv4"
assignee_type = "server"
auto_delete = false
}
resource "hcloud_primary_ip" "node_ipv6_addresses" {
for_each = local.hetzner_hosts
name = "primary_ipv6_${each.value.hostname}"
datacenter = each.value.datacenter
type = "ipv6"
assignee_type = "server"
auto_delete = false
}
# Create a server
resource "hcloud_server" "nodes" {
for_each = local.hetzner_hosts
name = each.value.hostname
datacenter = each.value.datacenter
image = each.value.image
server_type = each.value.server_type
ssh_keys = [hcloud_ssh_key.default.id]
user_data = "${data.template_file.cloud_init.rendered}"
public_net {
ipv4 = hcloud_primary_ip.node_ipv4_addresses[each.key].id
ipv6 = hcloud_primary_ip.node_ipv6_addresses[each.key].id
}
lifecycle {
ignore_changes = [
ssh_keys,
user_data
]
prevent_destroy = true
}
}
resource "hcloud_rdns" "nodes_rdns" {
for_each = local.hetzner_hosts
server_id = hcloud_server.nodes[each.key].id
ip_address = hcloud_server.nodes[each.key].ipv4_address
dns_ptr = each.value.rdns
}