-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
213 lines (182 loc) · 7.89 KB
/
main.tf
File metadata and controls
213 lines (182 loc) · 7.89 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
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
terraform {
required_version = ">= 1.5"
backend "gcs" {
bucket = "mento-terraform-tfstate-6ed6"
prefix = "monitoring-monorepo"
}
required_providers {
vercel = {
source = "vercel/vercel"
version = "~> 1.14"
}
upstash = {
source = "upstash/upstash"
version = "~> 1.4"
}
local = {
source = "hashicorp/local"
version = "~> 2.5"
}
}
}
# ── Providers ─────────────────────────────────────────────────────────────────
provider "vercel" {
api_token = var.vercel_token
team = var.vercel_team_id
}
provider "upstash" {
email = var.upstash_email
api_key = var.upstash_api_key
}
# ── Upstash Redis ─────────────────────────────────────────────────────────────
resource "upstash_redis_database" "address_labels" {
database_name = "address-labels"
region = "global"
primary_region = var.upstash_region
tls = true
}
locals {
# The Upstash provider may return just a hostname slug or a full URL.
# Normalise to a full HTTPS URL before embedding in env vars.
redis_rest_url = startswith(
upstash_redis_database.address_labels.endpoint, "https://"
) ? upstash_redis_database.address_labels.endpoint : "https://${upstash_redis_database.address_labels.endpoint}"
}
# ── Vercel Project ────────────────────────────────────────────────────────────
resource "vercel_project" "dashboard" {
name = "monitoring-dashboard"
framework = "nextjs"
team_id = var.vercel_team_id
# Deploy from monorepo root so pnpm-lock.yaml is included in the upload.
# Next.js is detected from ui-dashboard/ via root_directory.
root_directory = "ui-dashboard"
install_command = "pnpm install"
build_command = "pnpm build"
# No ignore_command — always build on every push.
# A "smart skip" caused production to be silently stuck for weeks.
git_repository = {
type = "github"
repo = "mento-protocol/monitoring-monorepo"
production_branch = "main"
}
}
# ── Environment Variables ─────────────────────────────────────────────────────
# Using individual resources so optional vars can use count without type-mixing.
resource "vercel_project_environment_variable" "hasura_url_multichain" {
count = var.hasura_url_multichain_hosted != "" ? 1 : 0
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "NEXT_PUBLIC_HASURA_URL_MULTICHAIN_HOSTED"
value = var.hasura_url_multichain_hosted
target = ["production", "preview"]
}
resource "vercel_project_environment_variable" "hasura_url_celo_sepolia" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "NEXT_PUBLIC_HASURA_URL_CELO_SEPOLIA_HOSTED"
value = var.hasura_url_celo_sepolia_hosted
target = ["production", "preview"]
}
resource "vercel_project_environment_variable" "hasura_url_monad_testnet" {
count = var.hasura_url_monad_testnet_hosted != "" ? 1 : 0
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "NEXT_PUBLIC_HASURA_URL_MONAD_TESTNET_HOSTED"
value = var.hasura_url_monad_testnet_hosted
target = ["production", "preview"]
}
resource "vercel_project_environment_variable" "redis_url" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "UPSTASH_REDIS_REST_URL"
value = local.redis_rest_url
target = ["production", "preview"]
}
resource "vercel_project_environment_variable" "redis_token" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "UPSTASH_REDIS_REST_TOKEN"
value = upstash_redis_database.address_labels.rest_token
target = ["production", "preview"]
sensitive = true
}
# Blob token is provisioned outside Terraform (no vercel_blob_store resource
# exists in the Vercel provider). Run `vercel blob create-store` once and add
# the resulting token to terraform.tfvars as `blob_read_write_token`.
resource "vercel_project_environment_variable" "blob_token" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "BLOB_READ_WRITE_TOKEN"
value = var.blob_read_write_token
target = ["production"]
sensitive = true
}
# ── Auth Environment Variables ────────────────────────────────────────────
resource "vercel_project_environment_variable" "auth_google_id" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "AUTH_GOOGLE_ID"
value = var.auth_google_id
target = ["production", "preview"]
sensitive = true
}
resource "vercel_project_environment_variable" "auth_google_secret" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "AUTH_GOOGLE_SECRET"
value = var.auth_google_secret
target = ["production", "preview"]
sensitive = true
}
resource "vercel_project_environment_variable" "auth_secret" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "AUTH_SECRET"
value = var.auth_secret
target = ["production", "preview"]
sensitive = true
}
resource "vercel_project_environment_variable" "cron_secret" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "CRON_SECRET"
value = var.cron_secret
# Production-only: preview deployments do not run cron jobs and should not
# have access to the backup trigger secret.
target = ["production"]
sensitive = true
}
# Preview auth proxy — routes Google OAuth through the prod domain (already
# whitelisted in GCP), then forwards the session back to the preview URL.
resource "vercel_project_environment_variable" "auth_redirect_proxy_url" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
key = "AUTH_REDIRECT_PROXY_URL"
value = "https://monitoring.mento.org/api/auth"
# Must be set on BOTH production and preview — Auth.js only enables proxy
# mode when this var is present in the stable (prod) env too.
# See: https://authjs.dev/getting-started/deployment#securing-a-preview-deployment
target = ["production", "preview"]
}
# Cron jobs are defined in ui-dashboard/vercel.json and activated automatically
# on first deploy. No Terraform resource is needed.
# ── Custom Domain ────────────────────────────────────────────────────────────
# DNS is already pointing to Vercel (CNAME → vercel-dns). Assigning the domain
# to this project is all that's needed.
resource "vercel_project_domain" "monitoring" {
project_id = vercel_project.dashboard.id
team_id = var.vercel_team_id
domain = "monitoring.mento.org"
}
# ── Local .vercel/project.json ────────────────────────────────────────────────
# Keeps the Vercel CLI linked to the correct project after creation/recreation.
# This file is gitignored but must exist locally for `vercel deploy` to work.
resource "local_file" "vercel_project_json" {
content = jsonencode({
projectId = vercel_project.dashboard.id
orgId = var.vercel_team_id
projectName = vercel_project.dashboard.name
})
filename = "${path.module}/../.vercel/project.json"
file_permission = "0644"
}