mykiwi/mykiwi.blogpublic Fork 0
ab57bea17065e93abbaa8a8c288357475e5ba1b2
Commits
Clone
git clone https://git.rickub.com/mykiwi/mykiwi.blog.git
git clone ssh://git@rickub.com/mykiwi/mykiwi.blog.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

main.tf · 90 lines · 3.0 KBTerraform Blame HistoryRaw
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago1# One Cloudflare Pages project + custom domain + DNS record per language,
2# matching blog/hugo.toml's multihost [languages.*] baseURLs: each language
3# is a fully separate site (public/<lang>/ from the Hugo build) deployed to
4# its own subdomain, with English at the zone apex.
Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago5#
6# The language codes themselves come from ../languages.txt, the same file
7# the Makefile reads for its deploy fan-out, so the two never drift apart.
8# blog/hugo.toml's [languages.*] blocks can't read that file too (Tofu has
9# no TOML parser to return the favor) - keep that one in sync by hand.
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago10locals {
Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago11 language_codes = split(" ", trimspace(file("${path.module}/../languages.txt")))
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago12 languages = {
Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago13 for code in local.language_codes : code => {
14 hostname = code == "en" ? var.zone_name : "${code}.${var.zone_name}"
15 project_name = "mykiwi-blog-${code}"
16 }
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago17 }
18}
19
20data "cloudflare_zone" "this" {
21 name = var.zone_name
22}
23
24# Direct-upload projects (no `source` block, i.e. no git integration) -
25# deploys happen out-of-band, e.g. `wrangler pages deploy public/en
26# --project-name=mykiwi-blog-en` from CI, since this repo lives on
27# tangled.sh rather than GitHub.
28resource "cloudflare_pages_project" "site" {
29 for_each = local.languages
30
31 account_id = var.cloudflare_account_id
32 name = each.value.project_name
33 production_branch = "main"
34}
35
36resource "cloudflare_pages_domain" "site" {
37 for_each = local.languages
38
39 account_id = var.cloudflare_account_id
40 project_name = cloudflare_pages_project.site[each.key].name
41 domain = each.value.hostname
42}
43
44resource "cloudflare_record" "site" {
45 for_each = local.languages
46
47 zone_id = data.cloudflare_zone.this.id
48 name = each.key == "en" ? "@" : each.key
49 type = "CNAME"
50 content = cloudflare_pages_project.site[each.key].subdomain
51 proxied = true
52}
53
54# www.mykiwi.blog -> mykiwi.blog (English apex), not its own language - just
55# the one common typo/habit redirect, not a 5th site.
56resource "cloudflare_record" "www" {
57 zone_id = data.cloudflare_zone.this.id
58 name = "www"
59 type = "CNAME"
60 content = var.zone_name
61 proxied = true
62}
63
Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 9h ago64# Page Rules would also do this but its Edit permission only shows up in
65# the API token builder for account Super Administrators - Single Redirects
66# (this resource) works with a normal scoped token (Zone:Dynamic Redirect:Edit).
67resource "cloudflare_ruleset" "www_redirect" {
68 zone_id = data.cloudflare_zone.this.id
69 name = "www to apex redirect"
70 description = "www.${var.zone_name} -> ${var.zone_name}, path preserved"
71 kind = "zone"
72 phase = "http_request_dynamic_redirect"
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago73
Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 9h ago74 rules {
75 ref = "www_redirect"
76 description = "Redirect www to apex"
77 expression = "(http.host eq \"www.${var.zone_name}\")"
78 action = "redirect"
79
80 action_parameters {
81 from_value {
82 status_code = 301
83 target_url {
84 expression = "concat(\"https://${var.zone_name}\", http.request.uri.path)"
85 }
86 preserve_query_string = true
87 }
Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago88 }
89 }
90}