Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/deploy-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ jobs:
terraform-validate:
name: Terraform validate
runs-on: ubuntu-latest
strategy:
matrix:
dir:
- terraform/github-pages
- terraform/cloudflare
defaults:
run:
working-directory: terraform/github-pages
working-directory: ${{ matrix.dir }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
84 changes: 84 additions & 0 deletions terraform/cloudflare/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}

# ---------------------------------------------------------------------------
# Variables
# ---------------------------------------------------------------------------

variable "cloudflare_api_token" {
description = "Cloudflare API token with Permissions: Zone:DNS:Edit, Account:Cloudflare Pages:Edit"
sensitive = true
}

variable "cloudflare_zone_id" {
description = "Cloudflare Zone ID for up2cloud.tech (Cloudflare dashboard → up2cloud.tech → Overview → Zone ID)"
}

# ---------------------------------------------------------------------------
# Locals
# ---------------------------------------------------------------------------

locals {
account_id = "6e6599da55818139812d41602175cffe"
project_name = "up2cloud-tech"
domain = "up2cloud.tech"
}

# ---------------------------------------------------------------------------
# Provider
# ---------------------------------------------------------------------------

provider "cloudflare" {
api_token = var.cloudflare_api_token
}

# ---------------------------------------------------------------------------
# Cloudflare Pages — register custom domain
# This tells Cloudflare Pages to serve up2cloud.tech from the up2cloud-tech project.
# ---------------------------------------------------------------------------

resource "cloudflare_pages_domain" "apex" {
account_id = local.account_id
project_name = local.project_name
domain = local.domain
}

resource "cloudflare_pages_domain" "www" {
account_id = local.account_id
project_name = local.project_name
domain = "www.${local.domain}"
}

# ---------------------------------------------------------------------------
# DNS records
# Cloudflare CNAME flattening makes a CNAME work for the apex domain.
# proxied = true routes traffic through Cloudflare edge (DDoS, WAF, cache).
# ---------------------------------------------------------------------------

resource "cloudflare_record" "apex_cname" {
zone_id = var.cloudflare_zone_id
name = "@"
type = "CNAME"
value = "${local.project_name}.pages.dev"
proxied = true
comment = "Cloudflare Pages — up2cloud.tech"

depends_on = [cloudflare_pages_domain.apex]
}

resource "cloudflare_record" "www_cname" {
zone_id = var.cloudflare_zone_id
name = "www"
type = "CNAME"
value = "${local.project_name}.pages.dev"
proxied = true
comment = "Cloudflare Pages — www.up2cloud.tech"

depends_on = [cloudflare_pages_domain.www]
}
Comment on lines +58 to +84
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When you associate a custom domain with a Cloudflare Pages project using cloudflare_pages_domain, Cloudflare automatically creates the corresponding DNS CNAME records in your zone (if the zone is managed in the same Cloudflare account).

Because of this automatic behavior, defining explicit cloudflare_record resources for the apex and www domains will cause terraform apply to fail with a Code: 81057 (An identical DNS record already exists) error when it tries to create them.

To resolve this, you should remove the explicit cloudflare_record resources entirely. Cloudflare Pages will handle the DNS record creation and proxying automatically.

Loading