| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 18h ago | 1 | # 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. |
| 5 | locals { |
| 6 | languages = { |
| 7 | en = { hostname = var.zone_name, project_name = "mykiwi-blog-en" } |
| 8 | fr = { hostname = "fr.${var.zone_name}", project_name = "mykiwi-blog-fr" } |
| 9 | pt = { hostname = "pt.${var.zone_name}", project_name = "mykiwi-blog-pt" } |
| 10 | ja = { hostname = "ja.${var.zone_name}", project_name = "mykiwi-blog-ja" } |
| Add Spanish (es) language support 479f7e2 Romain Gautier 14h ago | 11 | es = { hostname = "es.${var.zone_name}", project_name = "mykiwi-blog-es" } |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 18h ago | 12 | } |
| 13 | } |
| 14 | |
| 15 | data "cloudflare_zone" "this" { |
| 16 | name = var.zone_name |
| 17 | } |
| 18 | |
| 19 | # Direct-upload projects (no `source` block, i.e. no git integration) - |
| 20 | # deploys happen out-of-band, e.g. `wrangler pages deploy public/en |
| 21 | # --project-name=mykiwi-blog-en` from CI, since this repo lives on |
| 22 | # tangled.sh rather than GitHub. |
| 23 | resource "cloudflare_pages_project" "site" { |
| 24 | for_each = local.languages |
| 25 | |
| 26 | account_id = var.cloudflare_account_id |
| 27 | name = each.value.project_name |
| 28 | production_branch = "main" |
| 29 | } |
| 30 | |
| 31 | resource "cloudflare_pages_domain" "site" { |
| 32 | for_each = local.languages |
| 33 | |
| 34 | account_id = var.cloudflare_account_id |
| 35 | project_name = cloudflare_pages_project.site[each.key].name |
| 36 | domain = each.value.hostname |
| 37 | } |
| 38 | |
| 39 | resource "cloudflare_record" "site" { |
| 40 | for_each = local.languages |
| 41 | |
| 42 | zone_id = data.cloudflare_zone.this.id |
| 43 | name = each.key == "en" ? "@" : each.key |
| 44 | type = "CNAME" |
| 45 | content = cloudflare_pages_project.site[each.key].subdomain |
| 46 | proxied = true |
| 47 | } |
| 48 | |
| 49 | # www.mykiwi.blog -> mykiwi.blog (English apex), not its own language - just |
| 50 | # the one common typo/habit redirect, not a 5th site. |
| 51 | resource "cloudflare_record" "www" { |
| 52 | zone_id = data.cloudflare_zone.this.id |
| 53 | name = "www" |
| 54 | type = "CNAME" |
| 55 | content = var.zone_name |
| 56 | proxied = true |
| 57 | } |
| 58 | |
| Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 14h ago | 59 | # Page Rules would also do this but its Edit permission only shows up in |
| 60 | # the API token builder for account Super Administrators - Single Redirects |
| 61 | # (this resource) works with a normal scoped token (Zone:Dynamic Redirect:Edit). |
| 62 | resource "cloudflare_ruleset" "www_redirect" { |
| 63 | zone_id = data.cloudflare_zone.this.id |
| 64 | name = "www to apex redirect" |
| 65 | description = "www.${var.zone_name} -> ${var.zone_name}, path preserved" |
| 66 | kind = "zone" |
| 67 | phase = "http_request_dynamic_redirect" |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 18h ago | 68 | |
| Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 14h ago | 69 | rules { |
| 70 | ref = "www_redirect" |
| 71 | description = "Redirect www to apex" |
| 72 | expression = "(http.host eq \"www.${var.zone_name}\")" |
| 73 | action = "redirect" |
| 74 | |
| 75 | action_parameters { |
| 76 | from_value { |
| 77 | status_code = 301 |
| 78 | target_url { |
| 79 | expression = "concat(\"https://${var.zone_name}\", http.request.uri.path)" |
| 80 | } |
| 81 | preserve_query_string = true |
| 82 | } |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 18h ago | 83 | } |
| 84 | } |
| 85 | } |