tofu: rename from terraform, state in its own bucket, Loki buckets
All checks were successful
nixos / nixos (push) Successful in 34s

The infrastructure is applied with OpenTofu; name it so. terraform/
becomes tofu/, the files end in .tofu (OpenTofu 1.8+ prefers them), the
local variables file is secrets.auto.tfvars, and both servers carry
managed_by=opentofu (an in-place label change).

The state moved to new buckets asxpio-tofu-main/-repl, key
tofu/state/tofu.tfstate, with `tofu init -migrate-state`; a plan against
it shows no changes. The old asxpio-terraform-* buckets stay, renamed
in the config with moved blocks, until the new state has been used for
a while.

asxpio-loki-main, created by hand, is adopted with an import block, and
a new asxpio-loki-repl in fsn1 receives it in the daily replica, which
now also copies the new state bucket instead of the old one.

What keeps the Terraform name is OpenTofu's own: the `terraform {}`
block and .terraform.lock.hcl.

Assisted-by: Claude:opus-5-5
This commit is contained in:
Sergei Poljanski 2026-11-17 09:42:53 +04:00
commit 796331815d
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
14 changed files with 123 additions and 71 deletions

49
tofu/.gitignore vendored Normal file
View file

@ -0,0 +1,49 @@
# OpenTofu state files (the state itself is in Object Storage)
*.tfstate
*.tfstate.*
*.tfstate.backup
# Plan files
tfplan
*.tfplan
plan.txt
# Variable files with secrets (secrets.auto.tfvars)
*.tfvars
*.auto.tfvars
# CLI configuration
.terraformrc
terraform.rc
# Provider cache (the lock file is committed: it pins the providers)
.terraform/
# Crash log files
crash.log
crash.*.log
# Override files
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# SSH keys
*.pem
*.key
*_rsa
*_ed25519
deploy_key
deploy_key.pub
# IDE and editor files
.idea/
.vscode/
*.swp
*.swo
*~
# OS files
.DS_Store
Thumbs.db

68
tofu/.terraform.lock.hcl generated Normal file
View file

@ -0,0 +1,68 @@
# This file is maintained automatically by "tofu init".
# Manual edits may be lost in future updates.
provider "registry.opentofu.org/aminueza/minio" {
version = "3.46.3"
constraints = "~> 3.11"
hashes = [
"h1:BucOu+t3X1LSQzolHrWUE4+tPoMEi/C+fjeOLxilysA=",
"h1:CHKkCVQ4mARCFbTl3CHOKZg6tLDIXhE4xBTY9/H8998=",
"h1:DOOZypFT5G5fknb5U4Kp59WeYZU1OgiBOaZq2xljW1Q=",
"h1:JufolSPtuQatY/9D0+V3iplgvlVPqd2hoU5P4QIyziM=",
"h1:bmRxF3I9fP1xokjhQB0K2ujFtidd3UNp88k/DBjR1lE=",
"h1:c8Kzjx+0EnpV37xtpfoxI/7GckVrLx0dmIfDuAx2CF8=",
"h1:cJr90p040Vk8J8DVEKvTtxdDr+uLkcA4Na6gfJdLfeQ=",
"h1:cRpLvfM+mNNsvLIgeogpn2MKkko3v7HtcYEBYxP9elI=",
"h1:jQfBea+ntcQk0eIXfJodo8Yrvs+8R7+1ejidKaNZXR0=",
"h1:luMw2XWKS2a8yi/yIqn2qAw2rLAdAkNnbIHapkzpT9c=",
"h1:o+nzoc77gQN6pqPUGa1q17r35usD1k9LbIMiAibg4q4=",
"h1:syn1oZHCH6mydfTks8+aWfs2oWQNF3ii3HvSXuA5oJM=",
"h1:tUraLqzJBd5KAyxz2wVSSqMWc8oh3RmBbZaoFHvg+w0=",
"zh:01733c14f9d0724c404a258b668732f99f86ce16e65747902f7e51fb276fffe0",
"zh:127371a47bd643ca3a84523f1426c00e81195e3f10e30c7bdb8ca7987cc3c7ef",
"zh:1b27dad321fbdd550d328a63bc85093c72281a6af91ad6d249bbf15774dd91dd",
"zh:6c28ac386489ed904d1cf88382d21dba4ca749b1da6fcb1a3ecb11c0d6234f9f",
"zh:7cced05a3b2f26a03ad76cc13f655aab842f26db38a47a72020ff2ac7fcede47",
"zh:87cd6395207e20f532891902e44f2fdb92028669176cef7a8a30f1dc33d2e50f",
"zh:94c299b67007310e0e57892465ae54c9591749376916782e242dad3ecf9c7332",
"zh:95537fd6ccf778917c25e25fcc2c75a0599210db993e23dcf1b4c5c8eae7d234",
"zh:be52ecb44f323cce598e85084987948e2d3c6f07bf31250c372f28503831d4b8",
"zh:c3e0f2bdde045b27ea3ad14d65d5f595e2e48fa4b840c4fd1d107d3330fe49a0",
"zh:d63c68cc4cf30d3173a3207f95460d8882b79c3679ee51e45b008dc734df1d89",
"zh:ef6a3f439cc877133597251953cffd6ed3574039e1a4efd241f6ecb232baac5e",
"zh:f426a35a940ae70aa2c2931b691700c8285c8af49a2d8060df8031ed2a567ee9",
]
}
provider "registry.opentofu.org/hetznercloud/hcloud" {
version = "1.70.2"
constraints = "~> 1.70"
hashes = [
"h1:2eVvib40d9ki4E/wvPnT2wXxM704YjBRVxkozKHGTCs=",
"h1:BZWdJOileBlR+WMz0D0wrJXFhNLJpHcQJIgDqmPNlpw=",
"h1:EV7uBBPrEpNz8gJJ//iyBUu9/Tt+JJKjL3D3f9xVzX0=",
"h1:J5jjFM74jJrhwWKRdyQNFC3Ef7pbFBkLXGxbLSXheks=",
"h1:RYD1e858eOJstFN/bf9KonHtltMycZKIcYu7ngyiIRE=",
"h1:UB3bnUROLsLu1hreopppKw+Wm22//b3YZtM1k/SLloM=",
"h1:VB4xF79qoFHtcdxvEcajWgJZLJ59/uvbCReraaEHwqM=",
"h1:byNVX40XzpAO0MRuyxipatu4RWPfWKZ4Mf6PKyz0Tjk=",
"h1:cRkaW7N60DfWVvMhD8QtH2MdDDQzNOAJs0Gr5cmzwlE=",
"h1:eYR/chP/jFcSQlmPcfFBktyxZsoJuYRlJ57pOLVq1/k=",
"h1:hxmW37pH+dIDMH+9JQ9GSwjZg72SfvKIWUeJctmcy1M=",
"h1:qwPVmCN5bLh/2MjvsoatZhBvsvD8Nhs2J8OySiYN17M=",
"h1:wechRCpT64RPYT0BGWlkj/Y59BQa/qhO4elGaAlRpBo=",
"zh:05a474ecd2b019be03f857877c9bf1eca8f96c0897f6e76b91780adf3a8dd388",
"zh:1d4e4c0525c02618c015c079dc3e8d8b693ca796d698f95b59c4412114cbf166",
"zh:1f3bc703423f08a295e3e8f6d39c3f155e672da5e20b9a143ef7e7a465be37e3",
"zh:20ced3eec332b587a998ad3d87da322736aef49cb7e561a545a57f9a08000050",
"zh:619152b472b9ac266046db9d0c3f85946559ce0791bcb6fc78d278bb555d4572",
"zh:658a84427cc6b5385d68e1506678498dd7d3d04c0cf3410e3f2278c05b247ade",
"zh:6d9be3d79479345d21cef66e83002086153189535c82ab299d6ca5021aec7d0e",
"zh:72b0d26aadcc422ccb17e7a89abf92dfbe52dca6fc80025a934d7c4e058888cd",
"zh:865c0dc4912a5009f7b8c7ff374d47e496f9cfe96cbcebadc48f3434b7c12b46",
"zh:99f3f0778398d7348e5ab7dab42de0c918339a5250d140b6959272e50be4e372",
"zh:b3df6415bbe0f7021f94bff9e640c42e6d45b4a459ded3d267f10e2104a77760",
"zh:e7645b34e1aa45cb17dc473b33d7fa4d60e81d7e14332c7b04bc97535c867074",
"zh:f60b3e67be5cafe10001bb556ae7693e4a7696fb6946294643182946b36117f9",
]
}

105
tofu/README.md Normal file
View file

@ -0,0 +1,105 @@
# ASXP.IO Infrastructure
Hetzner Cloud infrastructure, managed with [OpenTofu](https://opentofu.org)
(`tofu`; the dev shell of `../nixos` has it). The files are plain HCL and
the state lives in Object Storage (`asxpio-tofu-main`, key
`tofu/state/tofu.tfstate`); `.terraform.lock.hcl` (OpenTofu keeps that
name) pins the providers and is committed. Files end in `.tofu`.
## Servers
- **asxpio-ops** (CX23: 2 vCPU, 4GB) - Traefik, Forgejo, app stacks
- **asxpio-services** (CX43: 8 vCPU, 16GB) - CI runner, game servers, SillyTavern
Both have delete and rebuild protection. Hetzner's server backups are
off; restic backs up the data instead (see ../README.md).
Both servers carry `prevent_destroy` and ignore changes to `user_data`,
`image` and `ssh_keys`, because hcloud would replace a running server
when those change.
A new server boots Debian and converts itself to NixOS: its cloud-init
(`cloud-init/nixos.yaml.tftpl`, one template for both) runs a pinned
nixos-infect and reboots into a bare NixOS, about three minutes after
creation. The system's configuration then comes from `../nixos`. Edits
to the template reach a server only when it is created or rebuilt in
place; both procedures are in `../nixos/README.md`.
## Storage (S3-compatible)
**OpenTofu state:**
- asxpio-tofu-main (hel1), replicated daily to asxpio-tofu-repl (fsn1)
- asxpio-terraform-main / -repl: where the state lived until 2026-11-17;
kept for now (`legacy_state_*`), to be emptied and removed
**Backups:**
- asxpio-backups-main (hel1), replicated to asxpio-backups-repl (fsn1),
except `restic/` (restic writes both itself)
**Logs:**
- asxpio-loki-main (hel1): Loki's chunks, replicated to asxpio-loki-repl
(fsn1)
The daily replica runs on asxpio-services
(`nixos/hosts/asxpio-services/replica.nix`).
## DNS
`dns.tofu` holds the zones asxp.io and czsk.it (Hetzner DNS, this project)
and every record set except SOA and NS. Our servers' records reference
`hcloud_server`, so moving a service is one edit.
All record sets are change-protected except the test XMPP servers'
(`prosody`, `ejabberd`, `anotherim`); both zones are delete-protected.
To change a protected record set, apply twice: first with the set
excluded from protection, then with the new value. Hand edits in the
console show up as drift in `tofu plan`.
## Network
- VPC: 10.10.0.0/20
- WireGuard VPN: 10.20.0.0/24
- IPv4 + IPv6 enabled
## Cost
| Item | Monthly |
|------|---------|
| ops CX23 | €3.99 |
| services CX43 | €11.99 |
| IPv4 (2x) | €1.00 |
| S3 (1TB) | €6.49 |
| S3 1TB Traffic | €1.00 |
| **Total** | **€24.47** |
Prices are net, as billed in November 2026 (0% VAT).
Both servers are billed below the current list price (€5.49 and €15.99).
A deleted and recreated server gets the list price, so reinstall or
rebuild in place and never recreate. `prevent_destroy` and Hetzner's
delete protection guard against it.
## Setup
```bash
cp secrets.auto.tfvars.example secrets.auto.tfvars # token and Object Storage keys
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... # state bucket
tofu init
tofu plan
tofu apply
```
In a new project the state bucket does not exist yet: comment out the
`backend "s3"` block in `providers.tofu` for the first apply, restore it,
then `tofu init -migrate-state`.
## Files
- `servers.tofu` - Server definitions, cloud-init variables
- `s3.tofu` - Object Storage buckets
- `vpc.tofu` - Private network
- `firewall.tofu` - Security rules
- `dns.tofu` - DNS zones and records
- `keys.tofu` - SSH keys
- `cloud-init/nixos.yaml.tftpl` - first boot: Debian to NixOS

View file

@ -0,0 +1,44 @@
#cloud-config
# First boot of the Debian image: convert it to NixOS in place with
# nixos-infect, then reboot. The result is a bare NixOS that keeps the
# image's partitions and the SSH host keys, with root's authorized_keys
# taken from the server's ssh_keys. Everything else comes from the flake:
#
# nixos-rebuild switch --flake ../nixos#${hostname} --target-host root@<ip>
#
# Log: /var/log/nixos-infect.log (after the reboot under /old-root).
hostname: ${hostname}
fqdn: ${hostname}.${domain}
manage_etc_hosts: true
ssh_deletekeys: false
write_files:
# Corrections to what nixos-infect generates, for this bootstrap
# generation only: the takeover of the old root (NIXOS_LUSTRATE) exists
# only in the scripted initrd; without an interface the IPv4 default
# route is never added (the gateway is not on the /32's link); and the
# private network gets an address but no route.
- path: /etc/nixos/bootstrap.nix
content: |
{ lib, ... }: {
boot.initrd.systemd.enable = false;
networking.defaultGateway = lib.mkForce {
address = "172.31.1.1";
interface = "eth0";
};
networking.interfaces.enp7s0.ipv4.routes = [
{ address = "${private_gateway}"; prefixLength = 32; }
{ address = "${private_net}"; prefixLength = ${private_prefix}; via = "${private_gateway}"; }
];
}
runcmd:
# Debian keeps /tmp in RAM; nixos-infect puts its swap file there
- >-
umount -l /tmp;
curl -fsSL -o /root/nixos-infect
https://raw.githubusercontent.com/elitak/nixos-infect/${infect_rev}/nixos-infect
&& echo "${infect_sha256} /root/nixos-infect" | sha256sum -c
&& PROVIDER=hetznercloud NIX_CHANNEL=${nix_channel} NIXOS_IMPORT=./bootstrap.nix
bash -x /root/nixos-infect > /var/log/nixos-infect.log 2>&1

150
tofu/dns.tofu Normal file
View file

@ -0,0 +1,150 @@
# DNS for asxp.io and czsk.it (Hetzner DNS, project asxpio). Every record
# set of both zones is here except SOA and NS, which Hetzner manages.
#
# Keys are "<name>/<type>"; "@" is the apex. Records of our own servers
# point at the server resources, so moving a service is one edit here.
# Every record set has Hetzner's change protection except those of the
# test servers in local.unprotected: a protected set cannot be changed or
# deleted until protection is lifted, so a change to one is two applies
# (set protection off for it, then edit). Both zones are delete-protected.
locals {
ip = {
ops_v4 = hcloud_server.ops.ipv4_address
ops_v6 = hcloud_server.ops.ipv6_address
services_v4 = hcloud_server.services.ipv4_address
services_v6 = hcloud_server.services.ipv6_address
}
zones = {
"asxp.io" = { ttl = 3600, delete_protection = true }
"czsk.it" = { ttl = 86400, delete_protection = true }
}
# Test XMPP servers (prosody, ejabberd, anotherim) and their subdomains
unprotected = "(^|\\.)(prosody|ejabberd|anotherim)$"
dns = {
"asxp.io" = {
"@/A" = { records = [local.ip.ops_v4] }
"anotherim/A" = { records = ["178.105.241.125"] }
"blog/A" = { records = [local.ip.ops_v4], ttl = 86400 }
"conference/A" = { records = [local.ip.services_v4], ttl = 300 }
"conference.anotherim/A" = { records = ["178.105.241.125"] }
"conference.ejabberd/A" = { records = ["178.105.240.144"] }
"conference.prosody/A" = { records = ["178.105.250.162"] }
"conference.xmpp/A" = { records = [local.ip.services_v4], ttl = 300 }
"ejabberd/A" = { records = ["178.105.240.144"] }
"mc/A" = { records = [local.ip.services_v4] }
"prosody/A" = { records = ["178.105.250.162"] }
"proxy.anotherim/A" = { records = ["178.105.241.125"] }
"proxy.ejabberd/A" = { records = ["178.105.240.144"] }
"proxy.prosody/A" = { records = ["178.105.250.162"] }
"pubsub.anotherim/A" = { records = ["178.105.241.125"] }
"pubsub.ejabberd/A" = { records = ["178.105.240.144"] }
"pubsub.prosody/A" = { records = ["178.105.250.162"] }
"s3/A" = { records = [local.ip.ops_v4] }
"traefik/A" = { records = [local.ip.ops_v4] }
"upload.ejabberd/A" = { records = ["178.105.240.144"] }
"upload.prosody/A" = { records = ["178.105.250.162"] }
"www/A" = { records = [local.ip.ops_v4] }
"xmpp/A" = { records = [local.ip.services_v4], ttl = 300 }
"@/AAAA" = { records = [local.ip.ops_v6] }
"anotherim/AAAA" = { records = ["2a01:4f8:c01e:27d9::1"] }
"blog/AAAA" = { records = [local.ip.ops_v6], ttl = 86400 }
"conference/AAAA" = { records = [local.ip.services_v6], ttl = 300 }
"conference.anotherim/AAAA" = { records = ["2a01:4f8:c01e:27d9::1"] }
"conference.ejabberd/AAAA" = { records = ["2a01:4f8:c01e:11b6::1"] }
"conference.prosody/AAAA" = { records = ["2a01:4f8:c01e:2891::1"] }
"conference.xmpp/AAAA" = { records = [local.ip.services_v6], ttl = 300 }
"ejabberd/AAAA" = { records = ["2a01:4f8:c01e:11b6::1"] }
"prosody/AAAA" = { records = ["2a01:4f8:c01e:2891::1"] }
"proxy.anotherim/AAAA" = { records = ["2a01:4f8:c01e:27d9::1"] }
"proxy.ejabberd/AAAA" = { records = ["2a01:4f8:c01e:11b6::1"] }
"proxy.prosody/AAAA" = { records = ["2a01:4f8:c01e:2891::1"] }
"pubsub.anotherim/AAAA" = { records = ["2a01:4f8:c01e:27d9::1"] }
"pubsub.ejabberd/AAAA" = { records = ["2a01:4f8:c01e:11b6::1"] }
"pubsub.prosody/AAAA" = { records = ["2a01:4f8:c01e:2891::1"] }
"s3/AAAA" = { records = [local.ip.ops_v6] }
"upload.ejabberd/AAAA" = { records = ["2a01:4f8:c01e:11b6::1"] }
"upload.prosody/AAAA" = { records = ["2a01:4f8:c01e:2891::1"] }
"www/AAAA" = { records = [local.ip.ops_v6] }
"xmpp/AAAA" = { records = [local.ip.services_v6], ttl = 300 }
"fm1._domainkey/CNAME" = { records = ["fm1.asxp.io.dkim.fmhosted.com."] }
"fm2._domainkey/CNAME" = { records = ["fm2.asxp.io.dkim.fmhosted.com."] }
"fm3._domainkey/CNAME" = { records = ["fm3.asxp.io.dkim.fmhosted.com."] }
"gf/A" = { records = [local.ip.ops_v4] }
"admin/A" = { records = [local.ip.ops_v4] }
"git/CNAME" = { records = ["git.czsk.it."], ttl = 86400 }
"mail/CNAME" = { records = ["mail.fastmail.com."] }
"@/MX" = { records = ["10 in1-smtp.messagingengine.com.", "20 in2-smtp.messagingengine.com."] }
"@/TXT" = { records = ["\"google-site-verification=H1sj22-DZ-4uW_Ri7z18RDKXo50bPwJXqIT1SyjthZg\"", "\"nfd=5777245\"", "\"v=spf1 include:spf.messagingengine.com ?all\""] }
"_discord/TXT" = { records = ["\"dh=8d39ced1e23afda4492219ee2456a81d209fecaa\""] }
"_dmarc/TXT" = { records = ["\"v=DMARC1;p=reject;rua=mailto:tr@asxp.io;ruf=mailto:tr@asxp.io;rf=afrf;pct=100\""] }
"mail._domainkey/TXT" = { records = ["\"k=rsa; t=s; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0X0brpvueSc7IiGZA2Qx zu12CWjeSN7/SquxrZM+afjU2If9BL5MHiE9iza/k6HgVnvc7exfMUdCHeLPUJJr WUIgCpYUR6ogJF0pe402zltYBExxjJFIMASAXuhTi0Gs4nc7fcfM56zSLckmJpO9 6jMrZPM7x3BxCeJt79j+4NLegClP8d5eRk1aByz9rujRYE\" \" rSJzaUcHuR47uEE5wz bSbY64hUadaSduBuD/OnAy0F3iqMSzVV5KNzDnAyWD4V7QcEWK7GAF9nuj3KhhS9 QLSHydPw/CCrhrXVjcprDDpfDG8dQw1JPfYPKs8h+Z44SGFw1ZxvqYIUmQ6mYrxv uwIDAQAB\""] }
"_stun._tcp/SRV" = { records = ["0 5 3478 xmpp.asxp.io."] }
"_stun._tcp.anotherim/SRV" = { records = ["0 5 3478 anotherim.asxp.io."] }
"_stun._tcp.ejabberd/SRV" = { records = ["0 5 3478 ejabberd.asxp.io."] }
"_stun._udp/SRV" = { records = ["0 5 3478 xmpp.asxp.io."] }
"_stun._udp.anotherim/SRV" = { records = ["0 5 3478 anotherim.asxp.io."] }
"_stun._udp.ejabberd/SRV" = { records = ["0 5 3478 ejabberd.asxp.io."] }
"_turn._tcp/SRV" = { records = ["0 5 3478 xmpp.asxp.io."] }
"_turn._tcp.anotherim/SRV" = { records = ["0 5 3478 anotherim.asxp.io."] }
"_turn._tcp.ejabberd/SRV" = { records = ["0 5 3478 ejabberd.asxp.io."] }
"_turn._udp/SRV" = { records = ["0 5 3478 xmpp.asxp.io."] }
"_turn._udp.anotherim/SRV" = { records = ["0 5 3478 anotherim.asxp.io."] }
"_turn._udp.ejabberd/SRV" = { records = ["0 5 3478 ejabberd.asxp.io."] }
"_xmpp-client._tcp/SRV" = { records = ["0 5 5222 xmpp.asxp.io."], ttl = 3600 }
"_xmpp-client._tcp.anotherim/SRV" = { records = ["0 5 5222 anotherim.asxp.io."] }
"_xmpp-client._tcp.ejabberd/SRV" = { records = ["0 5 5222 ejabberd.asxp.io."] }
"_xmpp-client._tcp.prosody/SRV" = { records = ["0 5 5222 prosody.asxp.io."] }
"_xmpp-server._tcp/SRV" = { records = ["0 5 5269 xmpp.asxp.io."], ttl = 3600 }
"_xmpp-server._tcp.anotherim/SRV" = { records = ["0 5 5269 anotherim.asxp.io."] }
"_xmpp-server._tcp.conference/SRV" = { records = ["0 5 5269 xmpp.asxp.io."], ttl = 3600 }
"_xmpp-server._tcp.conference.anotherim/SRV" = { records = ["0 5 5269 anotherim.asxp.io."] }
"_xmpp-server._tcp.conference.ejabberd/SRV" = { records = ["0 5 5269 ejabberd.asxp.io."] }
"_xmpp-server._tcp.conference.prosody/SRV" = { records = ["0 5 5269 prosody.asxp.io."] }
"_xmpp-server._tcp.ejabberd/SRV" = { records = ["0 5 5269 ejabberd.asxp.io."] }
"_xmpp-server._tcp.prosody/SRV" = { records = ["0 5 5269 prosody.asxp.io."] }
"_xmpps-client._tcp/SRV" = { records = ["0 5 5223 xmpp.asxp.io."], ttl = 3600 }
"_xmpps-client._tcp.anotherim/SRV" = { records = ["0 5 5223 anotherim.asxp.io."] }
"_xmpps-client._tcp.ejabberd/SRV" = { records = ["0 5 5223 ejabberd.asxp.io."] }
"_xmpps-client._tcp.prosody/SRV" = { records = ["0 5 5223 prosody.asxp.io."] }
},
"czsk.it" = {
"@/A" = { records = [local.ip.ops_v4] }
"www/A" = { records = [local.ip.ops_v4] }
"@/AAAA" = { records = [local.ip.ops_v6] }
}
}
# "<zone>/<name>/<type>" => record set; the key is also the import ID
rrsets = merge([
for zone, sets in local.dns : {
for key, set in sets : "${zone}/${key}" => merge(set, {
zone = zone
name = split("/", key)[0]
type = split("/", key)[1]
})
}
]...)
}
resource "hcloud_zone" "zone" {
for_each = local.zones
name = each.key
mode = "primary"
ttl = each.value.ttl
delete_protection = each.value.delete_protection
}
resource "hcloud_zone_rrset" "rr" {
for_each = local.rrsets
zone = hcloud_zone.zone[each.value.zone].name
name = each.value.name
type = each.value.type
ttl = try(each.value.ttl, null)
# A set can opt out with protected = false (e.g. to delete it next apply)
change_protection = try(each.value.protected, !can(regex(local.unprotected, each.value.name)))
records = [for value in each.value.records : { value = value }]
}

90
tofu/firewall.tofu Normal file
View file

@ -0,0 +1,90 @@
# Only ports of services that actually run are open to the internet.
# Everything else is reachable from the private network and WireGuard.
locals {
internal_networks = ["10.10.0.0/20", "10.20.0.0/24"] # VPC, WireGuard
anywhere = ["0.0.0.0/0", "::/0"]
ops_public = [
{ protocol = "tcp", port = "22" }, # git over SSH (Forgejo); shell users are limited by sshd
{ protocol = "tcp", port = "80" }, # Traefik
{ protocol = "tcp", port = "443" }, # Traefik
{ protocol = "udp", port = "60101" }, # WireGuard
]
services_public = [
{ protocol = "tcp", port = "25565" }, # Minecraft (Paper)
{ protocol = "udp", port = "19132" }, # Minecraft Bedrock (Geyser)
{ protocol = "tcp", port = "443" }, # Xray VLESS Reality
{ protocol = "tcp", port = "80" }, # ACME HTTP challenges, forwarded to Traefik on ops
{ protocol = "tcp", port = "5222-5223" }, # XMPP clients (STARTTLS, TLS)
{ protocol = "tcp", port = "5269" }, # XMPP servers
{ protocol = "tcp", port = "5443" }, # XMPP WebSocket, host-meta
{ protocol = "tcp", port = "7777" }, # XMPP file transfer proxy
{ protocol = "tcp", port = "8448" }, # Matrix federation (ejabberd)
{ protocol = "tcp", port = "3478" }, # STUN/TURN
{ protocol = "udp", port = "3478" }, # STUN/TURN
{ protocol = "udp", port = "49160-49199" }, # TURN relay
{ protocol = "udp", port = "50000-50099" }, # Telegram gateway calls
]
}
resource "hcloud_firewall" "ops_fw" {
name = "asxpio-ops-fw"
dynamic "rule" {
for_each = ["tcp", "udp"]
content {
direction = "in"
protocol = rule.value
port = "any"
source_ips = local.internal_networks
}
}
rule {
direction = "in"
protocol = "icmp"
source_ips = local.internal_networks
}
dynamic "rule" {
for_each = local.ops_public
content {
direction = "in"
protocol = rule.value.protocol
port = rule.value.port
source_ips = local.anywhere
}
}
}
resource "hcloud_firewall" "services_fw" {
name = "asxpio-services-fw"
dynamic "rule" {
for_each = ["tcp", "udp"]
content {
direction = "in"
protocol = rule.value
port = "any"
source_ips = local.internal_networks
}
}
rule {
direction = "in"
protocol = "icmp"
source_ips = local.internal_networks
}
dynamic "rule" {
for_each = local.services_public
content {
direction = "in"
protocol = rule.value.protocol
port = rule.value.port
source_ips = local.anywhere
}
}
}

9
tofu/keys.tofu Normal file
View file

@ -0,0 +1,9 @@
resource "hcloud_ssh_key" "asxpi" {
name = "asxpi"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC//ljrf65xbFyGmy/xeZs5GZvkmufCioqIGYguR3c8c/Ff9HiA4oMRT53Qs16KMuXRg62NusVQvJsvyPRSARpBFsoPiZ/YNBteWlqT1aVqeZwPOfUfFycQ2T8a9qtDnZ0WhamSlJMSAHQPkxXNA6S2ZE9y+dMzMc5qPeRwdsQmuvkxjual/35t9qTgCAG7LvKCKPJkLL5YpwLoqWYWTqSJ6RxNLTYNgIhJBZirzB5rNiFDDupnOuTKSpyRJNxyBSapNge5rCnGQYIeoJ4fi+FTi5wzjlC3hC0BN4Oc/zrm1nS9lyPXQS8KV5/vcQlPrvYZmv0LneeQ/gH/jJsSlwRp2qIatoASSfTlz1y1bOOYtPFojJPikXiXeZMCdlam1II6B94z1ybOIO8TqyKDVs4eHRFfYO72zv0sGsTr7t0C7+N9Wrpmhk38zdlLagZ6HO7KSyoAHm+fQc58fXmOhz4vfYrbmYRPgO0VO03fEb7lSFPPLaSseT0tayli05irJpc= asxpi@meow"
}
resource "hcloud_ssh_key" "deploy" {
name = "asxpio-deploy"
public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGTDPg3mV55lO8uBnvpcGRxJ2vMRP0qB3Pxhg+C8rwj9 asxpio-deploy"
}

37
tofu/outputs.tofu Normal file
View file

@ -0,0 +1,37 @@
# Server IPs
output "ops_ipv4" {
value = hcloud_server.ops.ipv4_address
description = "Ops server IPv4"
}
output "ops_ipv6" {
value = hcloud_server.ops.ipv6_address
description = "Ops server IPv6"
}
output "services_ipv4" {
value = hcloud_server.services.ipv4_address
description = "Services server IPv4"
}
output "services_ipv6" {
value = hcloud_server.services.ipv6_address
description = "Services server IPv6"
}
# VPC Network
output "vpc_network" {
value = {
id = hcloud_network.main.id
ip_range = hcloud_network.main.ip_range
}
description = "VPC network info"
}
# For a rebuild in place:
# tofu output -json user_data | jq -r '."asxpio-ops"' |
# hcloud server rebuild --image debian-13 --user-data-from-file - asxpio-ops
output "user_data" {
value = local.user_data
description = "cloud-init per server, as a new or rebuilt server gets it"
}

55
tofu/providers.tofu Normal file
View file

@ -0,0 +1,55 @@
# Run with OpenTofu (`tofu`, in the dev shell of ../nixos).
terraform {
required_version = ">= 1.8.0"
# State in Object Storage (bucket in s3.tofu). In a new project the
# bucket does not exist yet: see README.md, Setup.
backend "s3" {
bucket = "asxpio-tofu-main"
key = "tofu/state/tofu.tfstate"
region = "hel1"
endpoint = "https://hel1.your-objectstorage.com"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_region_validation = true
skip_requesting_account_id = true
use_path_style = true
}
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
# 1.54 no longer reads a server's location from the Hetzner API and
# plans to replace every server because of it. Do not go below 1.70.
version = "~> 1.70"
}
minio = {
source = "aminueza/minio"
version = "~> 3.11"
}
}
}
provider "hcloud" {
token = var.hcloud_token
}
# MinIO provider for Helsinki (hel1) region
provider "minio" {
alias = "hel1"
minio_server = var.s3_endpoint_hel1
minio_user = var.s3_access_key
minio_password = var.s3_secret_key
minio_region = "hel1"
minio_ssl = true
}
# MinIO provider for Falkenstein (fsn1) region
provider "minio" {
alias = "fsn1"
minio_server = var.s3_endpoint_fsn1
minio_user = var.s3_access_key
minio_password = var.s3_secret_key
minio_region = "fsn1"
minio_ssl = true
}

67
tofu/s3.tofu Normal file
View file

@ -0,0 +1,67 @@
# OpenTofu state (backend in providers.tofu), replicated hel1 -> fsn1
resource "minio_s3_bucket" "state_main" {
provider = minio.hel1
bucket = "asxpio-tofu-main"
acl = "private"
}
resource "minio_s3_bucket" "state_repl" {
provider = minio.fsn1
bucket = "asxpio-tofu-repl"
acl = "private"
}
# Where the state lived until 2026-11-17 (named after Terraform). Kept
# until the migrated state has been used for a while, then emptied and
# removed.
moved {
from = minio_s3_bucket.terraform_main
to = minio_s3_bucket.legacy_state_main
}
moved {
from = minio_s3_bucket.terraform_repl
to = minio_s3_bucket.legacy_state_repl
}
resource "minio_s3_bucket" "legacy_state_main" {
provider = minio.hel1
bucket = "asxpio-terraform-main"
acl = "private"
}
resource "minio_s3_bucket" "legacy_state_repl" {
provider = minio.fsn1
bucket = "asxpio-terraform-repl"
acl = "private"
}
resource "minio_s3_bucket" "backups_main" {
provider = minio.hel1
bucket = "asxpio-backups-main"
acl = "private"
}
resource "minio_s3_bucket" "backups_repl" {
provider = minio.fsn1
bucket = "asxpio-backups-repl"
acl = "private"
}
# Loki's chunks (asxpio-ops, services.loki). Created by hand on
# 2026-11-14, adopted here on 2026-11-17.
import {
to = minio_s3_bucket.loki_main
id = "asxpio-loki-main"
}
resource "minio_s3_bucket" "loki_main" {
provider = minio.hel1
bucket = "asxpio-loki-main"
acl = "private"
}
resource "minio_s3_bucket" "loki_repl" {
provider = minio.fsn1
bucket = "asxpio-loki-repl"
acl = "private"
}

View file

@ -0,0 +1,13 @@
# Hetzner Cloud API Token
# Get from: https://console.hetzner.cloud/ → Project → Security → API Tokens
hcloud_token = "your-hetzner-cloud-api-token-here"
# S3 Access Credentials
# Create buckets first in Hetzner Cloud Console, then get credentials
s3_access_key = "your-s3-access-key-here"
s3_secret_key = "your-s3-secret-key-here"
# S3 Endpoint (update after creating buckets)
# Format: https://<bucket-name>.fsn1.your-objectstorage.com
s3_endpoint_hel1 = "hel1.your-objectstorage.com"
s3_endpoint_fsn1 = "fsn1.your-objectstorage.com"

116
tofu/servers.tofu Normal file
View file

@ -0,0 +1,116 @@
# Debian on first boot installs NixOS over itself (cloud-init runs
# nixos-infect); the system itself is described in ../nixos.
locals {
user_data = {
for host in ["asxpio-ops", "asxpio-services"] : host => templatefile(
"${path.module}/cloud-init/nixos.yaml.tftpl", {
hostname = host
domain = "asxp.io"
# Pinned: this script replaces the operating system
infect_rev = "40f62a680bb0e8f2f607d79abfaaecd99d59401c"
infect_sha256 = "4354bd68773b41da65c0e815202c43c8549713b3ed3ff6381c71fbc0b0a840ab"
nix_channel = "nixos-26.05"
private_net = split("/", hcloud_network.main.ip_range)[0]
private_prefix = split("/", hcloud_network.main.ip_range)[1]
private_gateway = cidrhost(hcloud_network.main.ip_range, 1)
}
)
}
}
# Ops Server (2 vCPU, 4GB RAM)
resource "hcloud_server" "ops" {
name = "asxpio-ops"
image = "debian-13"
location = "hel1"
server_type = "cx23"
ssh_keys = [
hcloud_ssh_key.asxpi.id,
hcloud_ssh_key.deploy.id
]
# Off: restic backs up both hosts to object storage (ansible role
# restic_backup) for less than the 20% of the server price this costs
backups = false
delete_protection = true
rebuild_protection = true
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
network {
network_id = hcloud_network.main.id
ip = "10.10.0.10"
}
firewall_ids = [hcloud_firewall.ops_fw.id]
labels = {
role = "ops"
env = "production"
managed_by = "opentofu"
project = "asxpio"
}
# First boot converts the Debian image to NixOS (see local.user_data).
# hcloud replaces the server when user_data, image or ssh_keys change.
# Never let an edit to those destroy a running machine: they apply to
# a new server or to a rebuild in place (../nixos/README.md) only.
user_data = local.user_data["asxpio-ops"]
lifecycle {
ignore_changes = [user_data, image, ssh_keys]
prevent_destroy = true
}
}
# Services Server - CX43 (8 vCPU, 16GB RAM)
resource "hcloud_server" "services" {
name = "asxpio-services"
image = "debian-13"
location = "hel1"
server_type = "cx43"
ssh_keys = [
hcloud_ssh_key.asxpi.id,
hcloud_ssh_key.deploy.id
]
# Off: restic backs up both hosts to object storage (ansible role
# restic_backup) for less than the 20% of the server price this costs
backups = false
delete_protection = true
rebuild_protection = true
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
network {
network_id = hcloud_network.main.id
ip = "10.10.0.20"
}
firewall_ids = [hcloud_firewall.services_fw.id]
labels = {
role = "services"
env = "production"
managed_by = "opentofu"
project = "asxpio"
}
# First boot converts the Debian image to NixOS (see local.user_data).
# hcloud replaces the server when user_data, image or ssh_keys change.
# Never let an edit to those destroy a running machine: they apply to
# a new server or to a rebuild in place (../nixos/README.md) only.
user_data = local.user_data["asxpio-services"]
lifecycle {
ignore_changes = [user_data, image, ssh_keys]
prevent_destroy = true
}
}

29
tofu/variables.tofu Normal file
View file

@ -0,0 +1,29 @@
variable "hcloud_token" {
description = "Hetzner Cloud API Token"
type = string
sensitive = true
}
variable "s3_access_key" {
description = "S3 Access Key"
type = string
sensitive = true
}
variable "s3_secret_key" {
description = "S3 Secret Key"
type = string
sensitive = true
}
variable "s3_endpoint_hel1" {
description = "S3 Endpoint URL for Helsinki region"
type = string
default = "hel1.your-objectstorage.com"
}
variable "s3_endpoint_fsn1" {
description = "S3 Endpoint URL for Falkenstein region"
type = string
default = "fsn1.your-objectstorage.com"
}

11
tofu/vpc.tofu Normal file
View file

@ -0,0 +1,11 @@
resource "hcloud_network" "main" {
name = "asxpio-vpc"
ip_range = "10.10.0.0/20"
}
resource "hcloud_network_subnet" "eu-central" {
network_id = hcloud_network.main.id
type = "cloud"
network_zone = "eu-central"
ip_range = "10.10.0.0/23"
}