-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
280 lines (232 loc) · 7.9 KB
/
main.tf
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "4.50.0"
}
}
required_version = ">= 1.7.0"
}
variable "cloudflare_api_token" {
type = string
sensitive = true
description = "The Cloudflare API token."
}
variable "cloudflare_account_id" {
type = string
description = "The Cloudflare account id."
}
variable "cloudflare_zone_id" {
type = string
description = "The Cloudflare zone id."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/pages_project#required
variable "pages_project_name" {
type = string
description = "Name of the project."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/pages_domain
variable "pages_preview_domain" {
type = string
description = "Custom preview domain."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/pages_domain
variable "pages_production_domain" {
type = string
description = "Custom production domain."
}
variable "pages_preview_branch" {
type = string
default = "main"
description = "The name of the branch that is used for the preview environment."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/pages_project#required
variable "pages_production_branch" {
type = string
default = "production"
description = "The name of the branch that is used for the production environment."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/workers_kv_namespace
variable "kv_namespace" {
type = string
description = "Title value of the Worker KV Namespace."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/r2_bucket
variable "r2_bucket_name" {
type = string
description = "The name of the R2 bucket."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/r2_bucket#optional
variable "r2_bucket_location" {
type = string
default = "WEUR"
description = "The location hint of the R2 bucket."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/resources/d1_database
variable "d1_name" {
type = string
description = "The name of the D1 Database."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/4.44.0/docs/data-sources/zero_trust_access_identity_provider#required
variable "github_identity_provider_name" {
type = string
description = "GitHub Access Identity Provider name."
}
# see https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/zero_trust_access_policy#nestedatt--include--github_organization
variable "github_organization" {
type = string
description = "The name of the organization."
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
resource "cloudflare_workers_kv_namespace" "preview" {
account_id = var.cloudflare_account_id
title = "${var.kv_namespace}-preview"
}
resource "cloudflare_workers_kv_namespace" "production" {
account_id = var.cloudflare_account_id
title = "${var.kv_namespace}-production"
}
resource "cloudflare_r2_bucket" "preview" {
account_id = var.cloudflare_account_id
name = "${var.r2_bucket_name}-preview"
location = var.r2_bucket_location
}
resource "cloudflare_r2_bucket" "production" {
account_id = var.cloudflare_account_id
name = "${var.r2_bucket_name}-production"
location = var.r2_bucket_location
}
resource "cloudflare_d1_database" "preview" {
account_id = var.cloudflare_account_id
name = "${var.d1_name}-preview"
}
resource "cloudflare_d1_database" "production" {
account_id = var.cloudflare_account_id
name = "${var.d1_name}-production"
}
resource "cloudflare_pages_project" "page" {
account_id = var.cloudflare_account_id
name = var.pages_project_name
production_branch = var.pages_production_branch
lifecycle {
ignore_changes = [build_config]
}
}
resource "cloudflare_pages_domain" "preview" {
account_id = var.cloudflare_account_id
project_name = var.pages_project_name
domain = var.pages_preview_domain
depends_on = [cloudflare_pages_project.page]
}
resource "cloudflare_pages_domain" "production" {
account_id = var.cloudflare_account_id
project_name = var.pages_project_name
domain = var.pages_production_domain
depends_on = [cloudflare_pages_project.page]
}
resource "cloudflare_record" "preview" {
zone_id = var.cloudflare_zone_id
name = cloudflare_pages_domain.preview.domain
content = "${var.pages_preview_branch}.${cloudflare_pages_project.page.subdomain}"
type = "CNAME"
proxied = true
}
resource "cloudflare_record" "production" {
zone_id = var.cloudflare_zone_id
name = cloudflare_pages_domain.production.domain
content = cloudflare_pages_project.page.subdomain
type = "CNAME"
proxied = true
}
data "cloudflare_zero_trust_access_identity_provider" "idp" {
account_id = var.cloudflare_account_id
name = var.github_identity_provider_name
}
resource "cloudflare_zero_trust_access_service_token" "e2e" {
account_id = var.cloudflare_account_id
name = "${cloudflare_pages_domain.production.domain} End-to-End"
duration = "8760h"
min_days_for_renewal = 30
}
resource "cloudflare_zero_trust_access_policy" "github_organization" {
account_id = var.cloudflare_account_id
name = "Allow GitHub organization"
decision = "allow"
include {
github {
identity_provider_id = data.cloudflare_zero_trust_access_identity_provider.idp.id
name = var.github_organization
}
}
}
resource "cloudflare_zero_trust_access_policy" "e2e" {
account_id = var.cloudflare_account_id
name = "Allow End-to-End service token"
decision = "non_identity"
include {
service_token = [cloudflare_zero_trust_access_service_token.e2e.id]
}
}
resource "cloudflare_zero_trust_access_application" "preview" {
account_id = var.cloudflare_account_id
name = cloudflare_pages_domain.preview.domain
domain = cloudflare_pages_domain.preview.domain
type = "self_hosted"
session_duration = "24h"
auto_redirect_to_identity = true
http_only_cookie_attribute = true
same_site_cookie_attribute = "lax"
allowed_idps = [data.cloudflare_zero_trust_access_identity_provider.idp.id]
policies = [
cloudflare_zero_trust_access_policy.github_organization.id,
cloudflare_zero_trust_access_policy.e2e.id
]
destinations {
uri = cloudflare_pages_domain.preview.domain
}
destinations {
uri = "*.${cloudflare_pages_project.page.subdomain}"
}
}
output "domain_page" {
value = cloudflare_pages_project.page.subdomain
}
output "domain_preview" {
value = cloudflare_pages_domain.preview.domain
}
output "domain_production" {
value = cloudflare_pages_domain.production.domain
}
output "kv_preview" {
value = cloudflare_workers_kv_namespace.preview.id
}
output "kv_production" {
value = cloudflare_workers_kv_namespace.production.id
}
output "r2_preview" {
value = cloudflare_r2_bucket.preview.id
}
output "r2_production" {
value = cloudflare_r2_bucket.production.id
}
output "d1_preview" {
value = cloudflare_d1_database.preview.id
}
output "d1_production" {
value = cloudflare_d1_database.production.id
}
output "jwt_audience" {
value = cloudflare_zero_trust_access_application.preview.aud
}
output "e2e_client_id" {
value = cloudflare_zero_trust_access_service_token.e2e.client_id
}
output "e2e_client_secret" {
value = cloudflare_zero_trust_access_service_token.e2e.client_secret
sensitive = true
}
output "web_analytics_token" {
value = cloudflare_pages_project.page.build_config[0].web_analytics_token
}