infrastructure/hcloud.tf

65 lines
1.8 KiB
Terraform
Raw Normal View History

2024-09-28 01:24:03 +00:00
resource "hcloud_ssh_key" "default" {
name = var.default_ssh_key.name
public_key = var.default_ssh_key.public_key
2024-09-25 11:23:52 +00:00
}
data "template_file" "cloud_init" {
template = "${file("./tf-templates/cloud-init.yaml.tpl")}"
vars = {
tailscale_authkey = "${tailscale_tailnet_key.cloud_init_key.key}"
2024-09-28 01:24:03 +00:00
default_ssh_key = var.default_ssh_key.public_key
2024-09-25 11:23:52 +00:00
}
}
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
2024-09-28 01:24:03 +00:00
ssh_keys = [hcloud_ssh_key.default.id]
2024-09-25 11:23:52 +00:00
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
]
2024-09-26 23:51:58 +00:00
prevent_destroy = true
2024-09-25 11:23:52 +00:00
}
}
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
}