-
Notifications
You must be signed in to change notification settings - Fork 0
fix(dns): add Cloudflare DNS terraform to restore up2cloud.tech #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_recordresources for the apex andwwwdomains will causeterraform applyto fail with aCode: 81057 (An identical DNS record already exists)error when it tries to create them.To resolve this, you should remove the explicit
cloudflare_recordresources entirely. Cloudflare Pages will handle the DNS record creation and proxying automatically.