diff --git a/terraform/dns.tf b/terraform/dns.tf index bd2df4c..fd90327 100644 --- a/terraform/dns.tf +++ b/terraform/dns.tf @@ -1,3 +1,18 @@ # DNS layer: A record for the apex domain pointing at the server's public IPv4. # The zone is at Cloudflare; Caddy does its own ACME challenge, so no # additional records are needed. +# +# proxied = false on purpose: an orange-cloud record terminates TLS at +# Cloudflare and Caddy can never complete its ACME challenge. + +provider "cloudflare" {} + +resource "cloudflare_dns_record" "apex" { + zone_id = var.cloudflare_zone_id + name = var.domain + type = "A" + content = hcloud_server.server0.ipv4_address + ttl = 300 + proxied = false + comment = "server0, managed by Terraform" +} diff --git a/terraform/main.tf b/terraform/main.tf index 4497790..8fce81c 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,6 +1,28 @@ -# Server layer: packages, swap, containerd, kubeadm control plane, Calico, -# metrics-server, local-path provisioner, and Caddy. +# Server layer: creates server0 at Hetzner and installs the SSH key at boot. +# Packages, swap, containerd, kubeadm, addons and Caddy follow in later steps +# over SSH; see plan.md. # # Terraform does not manage anything inside the cluster beyond the bootstrap # (namespace cv, quota, addons). Workloads belong to kustomize. -# See OKF_CV_Website/architecture/terraform.md. + +provider "hcloud" {} + +resource "hcloud_ssh_key" "cv_infra" { + name = "cv-infra" + public_key = file(pathexpand(var.ssh_public_key_path)) +} + +resource "hcloud_server" "server0" { + name = "server0" + server_type = var.server_type + image = var.server_image + location = var.server_location + ssh_keys = [hcloud_ssh_key.cv_infra.id] + labels = { role = "cv-control-plane" } + + public_net { + ipv4_enabled = true + # No AAAA record is managed, so do not pay for an unused IPv6. + ipv6_enabled = false + } +} diff --git a/terraform/outputs.tf b/terraform/outputs.tf index dc6b7d4..ff03d4c 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -2,7 +2,12 @@ output "server_ip" { description = "Public IPv4 of server0." - value = var.server_host + value = hcloud_server.server0.ipv4_address +} + +output "ssh_command" { + description = "Ready-to-paste SSH command for server0." + value = "ssh -i ${var.ssh_private_key_path} ${var.ssh_user}@${hcloud_server.server0.ipv4_address}" } output "proxy_url" { diff --git a/terraform/plan.md b/terraform/plan.md index 50d8f47..42e2177 100644 --- a/terraform/plan.md +++ b/terraform/plan.md @@ -20,12 +20,14 @@ on an empty machine. See `OKF_CV_Website/architecture/terraform.md`. ## Steps -### 1. Server source +### 1. Server source — done -Decide the two open questions above. Write them into this file so later steps -have a fixed target. +`main.tf` creates `server0` as a Hetzner `hcloud_server` (`cx22`, `ubuntu-24.04`) +and registers `~/.ssh/cv_infra.pub` as an `hcloud_ssh_key`, so the key is in +`authorized_keys` before the machine boots. `terraform.md` and `README.md` still +describe `server_host` as an input; they need updating. -Check: one sentence answers "what does `apply` create?". +Check: `terraform output server_ip` returns the address. ### 2. SSH key — done @@ -34,11 +36,10 @@ server during creation (step 6), not after. Check: `ssh-keygen -y -f ~/.ssh/cv_infra` prints the public key. -### 3. Provider and versions +### 3. Provider and versions — done -Uncomment `versions.tf`, set `required_version` and the provider(s), run -`terraform init`. Commit `.terraform.lock.hcl` is currently ignored — decide -whether that stays. +`versions.tf` pins `cloudflare` 5.28.1 and `hcloud` 1.70.2; init and validate +pass. `required_version` and a `null` provider still need adding before step 7. Check: `terraform init` succeeds, `.terraform.lock.hcl` exists. @@ -50,20 +51,22 @@ Copy `terraform.tfvars.example` to `terraform.tfvars`, fill `server_host`, Check: `terraform validate` passes. -### 5. DNS first +### 5. DNS — written -Write `dns.tf`: one `cloudflare_record` for the apex A record. Smallest real -resource, and it exercises provider, token and zone ID with no server involved. +`dns.tf` creates `cloudflare_dns_record.apex` from +`hcloud_server.server0.ipv4_address`, so the record can only be created after +the server — one `apply` does both, in order. `proxied = false` is required for +Caddy's ACME challenge. Check: `terraform apply` creates the record, `dig +short ` returns the server IP. -### 6. Server creation +### 6. Server creation — written -Add the server resource with the SSH public key attached at creation. +`main.tf` creates the server with the SSH public key attached at creation. -Check: `terraform apply` creates it, `ssh -i ~/.ssh/cv_infra @` -works without a password. +Check: `terraform apply` creates it, `terraform output ssh_command` works +without a password. ### 7. Connection skeleton diff --git a/terraform/terraform.tfvars.example b/terraform/terraform.tfvars.example index 002fcf1..868b4f9 100644 --- a/terraform/terraform.tfvars.example +++ b/terraform/terraform.tfvars.example @@ -1,6 +1,9 @@ # Copy to terraform.tfvars and fill in. terraform.tfvars is Git-ignored. -server_host = "" ssh_user = "root" -ssh_private_key_path = "~/.ssh/id_ed25519" +ssh_private_key_path = "~/.ssh/cv_infra" +ssh_public_key_path = "~/.ssh/cv_infra.pub" +server_type = "cx22" +server_image = "ubuntu-24.04" +server_location = "nbg1" domain = "example.dev" cloudflare_zone_id = "" diff --git a/terraform/variables.tf b/terraform/variables.tf index a8c8652..ab4d426 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,10 +1,8 @@ # Inputs: the server, its SSH access, and the domain. # Nothing secret belongs here; secrets come from the environment or tfvars. - -variable "server_host" { - description = "Public IP or hostname of server0." - type = string -} +# +# server_host is gone: Terraform now creates the server, so its IP is an output +# rather than an input. variable "ssh_user" { description = "SSH user Terraform logs in as." @@ -17,6 +15,30 @@ variable "ssh_private_key_path" { type = string } +variable "ssh_public_key_path" { + description = "Path to the matching public key, installed at server creation." + type = string + default = "~/.ssh/cv_infra.pub" +} + +variable "server_type" { + description = "Hetzner server type. cx22 gives 2 vCPU and 4 GB, the floor for a control plane." + type = string + default = "cx22" +} + +variable "server_image" { + description = "Hetzner image for the base system." + type = string + default = "ubuntu-24.04" +} + +variable "server_location" { + description = "Hetzner location, e.g. nbg1, fsn1, hel1." + type = string + default = "nbg1" +} + variable "domain" { description = "Apex domain served by the site, e.g. noelsabia.dev." type = string