iac/terraform/README.md
Sergei Poljanski c51522d371
terraform: install NixOS at first boot, run with OpenTofu
A new or rebuilt server should come up as NixOS without a manual
install. cloud-init now runs a pinned nixos-infect on the Debian image
and reboots into a bare NixOS; the system itself comes from nixos/.
One template replaces the two near-identical Debian cloud-init files.

nixos-infect's output needs three corrections on NixOS 26.05, written
to bootstrap.nix: the takeover of the old root only exists in the
scripted initrd, the IPv4 default route is never added without an
interface, and the private network gets no route. Found and verified
on throwaway servers.

The servers still ignore changes to user_data, so nothing happens to
the running ones. The new user_data output feeds a rebuild in place.

Switch to OpenTofu: same HCL and state, and it is free software in
nixpkgs. Commit the provider lock file so everyone gets the same
providers.

Assisted-by: Claude:fable-5-2
2026-11-16 22:59:58 +04:00

102 lines
3.2 KiB
Markdown

# 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 is the one Terraform wrote; `.terraform.lock.hcl` pins the
providers and is committed.
## 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)
**Terraform State:**
- asxpio-terraform-main (hel1)
- asxpio-terraform-repl (fsn1)
**Backups:**
- asxpio-backups-main (hel1)
- asxpio-backups-repl (fsn1)
**Logs:**
- asxpio-loki-main (hel1): Loki chunks; created by hand, not in Terraform
## DNS
`dns.tf` 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 `terraform 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 terraform.tfvars.example terraform.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.tf` for the first apply, restore it,
then `tofu init -migrate-state`.
Coming from Terraform: `tofu init -reconfigure` once; nothing in the
state changes.
## Files
- `servers.tf` - Server definitions, cloud-init variables
- `s3.tf` - S3 buckets (6 total)
- `vpc.tf` - Private network
- `firewall.tf` - Security rules
- `dns.tf` - DNS zones and records
- `keys.tf` - SSH keys
- `cloud-init/nixos.yaml.tftpl` - first boot: Debian to NixOS