-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
105 lines (95 loc) · 3.13 KB
/
Copy pathmain.tf
File metadata and controls
105 lines (95 loc) · 3.13 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# define our dns zone
locals {
dns_zone_name = "jaseblenner.com"
github_owner = "jaseblenner" # name of our github account/org which our pages project codebase belongs to
github_repo = "wwwjaseblennercom" # name ofthe github repo which our cloudflare pages project codebase belongs to
}
# retrieve info from the (existing) dns zone in cloudflare
data "cloudflare_zone" "zone" {
name = local.dns_zone_name
}
# Create Cloudflare Pages project
resource "cloudflare_pages_project" "wwwjaseblennercom" {
account_id = var.cloudflare_master_account_id
name = var.project_name
production_branch = "main"
source {
type = "github"
config {
owner = local.github_owner
repo_name = local.github_repo
production_branch = "main"
pr_comments_enabled = true
deployments_enabled = true
# we dont auto deploy to our production domain (ie. jaseblenner.com)
production_deployment_enabled = false
# we deploy to our dev domain from all branches except "main"
preview_deployment_setting = "custom"
preview_branch_excludes = ["main"]
preview_branch_includes = ["*"]
}
}
build_config {
build_command = "npm run build"
destination_dir = "out"
root_dir = "app"
}
deployment_configs {
preview {
environment_variables = {
NODE_VERSION = "17"
}
}
production {
environment_variables = {
NODE_VERSION = "17"
}
}
}
}
## Associate our Cloudflare Pages Project with our existing (jaseblenner.com) domain
resource "cloudflare_pages_domain" "wwwjaseblennercom" {
account_id = var.cloudflare_master_account_id
project_name = var.project_name
domain = data.cloudflare_zone.zone.name
}
## Point our root domain at the cloudflare pages CNAME
resource "cloudflare_record" "jaseblennercom" {
zone_id = data.cloudflare_zone.zone.zone_id
name = "@"
value = cloudflare_pages_project.wwwjaseblennercom.subdomain
type = "CNAME"
ttl = 60
}
## Redirect www to our domain apex
## Ref: https://developers.cloudflare.com/pages/how-to/www-redirect/
resource "cloudflare_record" "wwwjaseblennercom" {
zone_id = data.cloudflare_zone.zone.zone_id
name = "www"
value = "192.0.2.1"
type = "A"
ttl = 1
proxied = true
}
# Redirect http to https
resource "cloudflare_ruleset" "wwwjaseblennercom" {
zone_id = data.cloudflare_zone.zone.zone_id
name = "www_redirect_to_https"
kind = "zone"
phase = "http_request_dynamic_redirect"
rules {
action = "redirect"
action_parameters {
from_value {
status_code = 301
target_url {
value = "https://${cloudflare_record.jaseblennercom.hostname}"
}
preserve_query_string = true
}
}
expression = "(http.host eq \"${cloudflare_record.wwwjaseblennercom.hostname}\")"
description = "Redirect ${cloudflare_record.wwwjaseblennercom.hostname} to https://${cloudflare_record.jaseblennercom.hostname}"
enabled = true
}
}