mykiwi/mykiwi.blogpublic Fork 0
479f7e2baff211e8f6ca4d3441f31da4f33f39aa
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.

Add Spanish (es) language support 479f7e2 · on 479f7e2baff211e8f6ca4d3441f31da4f33f39aa · Romain Gautier · 9h ago
main.tf · 70 lines · 2.2 KBTerraform Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# One Cloudflare Pages project + custom domain + DNS record per language,
# matching blog/hugo.toml's multihost [languages.*] baseURLs: each language
# is a fully separate site (public/<lang>/ from the Hugo build) deployed to
# its own subdomain, with English at the zone apex.
locals {
  languages = {
    en = { hostname = var.zone_name, project_name = "mykiwi-blog-en" }
    fr = { hostname = "fr.${var.zone_name}", project_name = "mykiwi-blog-fr" }
    pt = { hostname = "pt.${var.zone_name}", project_name = "mykiwi-blog-pt" }
    ja = { hostname = "ja.${var.zone_name}", project_name = "mykiwi-blog-ja" }
    es = { hostname = "es.${var.zone_name}", project_name = "mykiwi-blog-es" }
  }
}

data "cloudflare_zone" "this" {
  name = var.zone_name
}

# Direct-upload projects (no `source` block, i.e. no git integration) -
# deploys happen out-of-band, e.g. `wrangler pages deploy public/en
# --project-name=mykiwi-blog-en` from CI, since this repo lives on
# tangled.sh rather than GitHub.
resource "cloudflare_pages_project" "site" {
  for_each = local.languages

  account_id        = var.cloudflare_account_id
  name              = each.value.project_name
  production_branch = "main"
}

resource "cloudflare_pages_domain" "site" {
  for_each = local.languages

  account_id   = var.cloudflare_account_id
  project_name = cloudflare_pages_project.site[each.key].name
  domain       = each.value.hostname
}

resource "cloudflare_record" "site" {
  for_each = local.languages

  zone_id = data.cloudflare_zone.this.id
  name    = each.key == "en" ? "@" : each.key
  type    = "CNAME"
  content = cloudflare_pages_project.site[each.key].subdomain
  proxied = true
}

# www.mykiwi.blog -> mykiwi.blog (English apex), not its own language - just
# the one common typo/habit redirect, not a 5th site.
resource "cloudflare_record" "www" {
  zone_id = data.cloudflare_zone.this.id
  name    = "www"
  type    = "CNAME"
  content = var.zone_name
  proxied = true
}

resource "cloudflare_page_rule" "www_redirect" {
  zone_id  = data.cloudflare_zone.this.id
  target   = "www.${var.zone_name}/*"
  priority = 1

  actions {
    forwarding_url {
      url         = "https://${var.zone_name}/$1"
      status_code = 301
    }
  }
}