-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.tf
465 lines (403 loc) · 13.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
### SETUP ###
provider "google" {
project = var.project_id
region = var.region
}
# Enable APIs
resource "google_project_service" "run_api" {
service = "run.googleapis.com"
}
resource "google_project_service" "schedule_api" {
service = "cloudscheduler.googleapis.com"
}
# Set up service account and permissions
resource "google_service_account" "cloud_run_sa" {
account_id = "snowplow-ice-terra"
display_name = "Snowplow Cloud Run Service Account"
}
resource "google_project_iam_member" "cloud_run_sa" {
for_each = toset([
"roles/bigquery.dataEditor",
"roles/bigquery.jobUser",
"roles/pubsub.editor",
"roles/run.serviceAgent",
"roles/run.invoker",
"roles/logging.logWriter",
"roles/storage.objectViewer"
])
project = var.project_id
role = each.value
member = google_service_account.cloud_run_sa.member
}
### VARIABLES ###
locals {
job_timeout = "300s"
dbt_repo_url = "https://github.com/dumkydewilde/snowplow-serverless.git"
dbt_repo_folder_name = "snowplow-serverless/dbt"
# enrichments
campaign_attribution = file("${path.module}/configs/enricher/campaign_attribution.json")
anonymise_ip = file("${path.module}/configs/enricher/anon_ip.json")
referer_parser = file("${path.module}/configs/enricher/referer_parser.json")
javascript_enrichment = templatefile("${path.module}/configs/enricher/javascript_enrichment.json.tmpl", {
javascript_script = base64encode(file("${path.module}/configs/enricher/javascript_enrichment_script.js"))
})
yauaa_enrichment = file("${path.module}/configs/enricher/yauaa_enrichment.json")
enrichments_list = [
local.campaign_attribution,
local.anonymise_ip,
local.referer_parser,
local.javascript_enrichment,
local.yauaa_enrichment
]
}
### IGLU SCHEMAS ###
resource "random_string" "six_chars" {
length = 6
special = false
upper = false
}
resource "google_storage_bucket" "iglu_resolver_bucket" {
name = "${var.prefix}-iglu-schemas-${random_string.six_chars.result}"
location = var.region
project = var.project_id
uniform_bucket_level_access = true
force_destroy = true
}
# Make the bucket public
resource "google_storage_bucket_iam_member" "member" {
bucket = google_storage_bucket.iglu_resolver_bucket.name
role = "roles/storage.objectViewer"
member = "allUsers"
}
resource "google_storage_bucket_object" "schemas" {
for_each = fileset("${path.module}/schemas/", "**")
source = "${path.module}/schemas/${each.value}"
name = "schemas/${each.value}"
content_type = "text/plain"
bucket = google_storage_bucket.iglu_resolver_bucket.id
}
locals {
config_iglu_resolver = base64encode(templatefile("${path.module}/configs/iglu_resolver.json.tmpl", {
VENDOR = var.vendor
BUCKET_NAME = google_storage_bucket.iglu_resolver_bucket.name
BUCKET_PATH = ""
})
)
}
### PIPELINE ###
# 1. Deploy PubSub Topics & Subs
locals {
# Configs
topic_names = ["raw", "bad", "enriched", "bq-bad-rows", "loader-types", "failed-inserts"]
}
resource "google_pubsub_topic" "topics" {
for_each = toset(local.topic_names)
name = "${var.prefix}-${each.value}-topic"
labels = var.labels
}
resource "google_pubsub_subscription" "subscriptions" {
for_each = toset(local.topic_names)
name = "${var.prefix}-${each.value}-sub"
topic = google_pubsub_topic.topics[each.value].name
expiration_policy {
ttl = ""
}
labels = var.labels
depends_on = [ google_pubsub_topic.topics ]
}
# 2. Collector
locals {
config_collector = base64encode(templatefile("${path.module}/configs/collector/config.hocon.tmpl", {
stream_good = google_pubsub_topic.topics["raw"].name
stream_bad = google_pubsub_topic.topics["bad"].name
google_project_id = var.project_id
}))
}
resource "google_cloud_run_v2_service" "collector_server" {
name = "${var.prefix}-collector-server"
location = var.region
project = var.project_id
ingress = "INGRESS_TRAFFIC_ALL"
template {
service_account = google_service_account.cloud_run_sa.email
scaling {
max_instance_count = 1
}
containers {
name = "${var.prefix}-collector-server"
image = "snowplow/scala-stream-collector-pubsub:latest"
command = [
"/bin/sh",
"-c",
"echo '${local.config_collector}' | base64 -d > config.hocon && /home/snowplow/bin/snowplow-stream-collector --config=config.hocon"
]
}
}
traffic {
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
percent = 100
}
depends_on = [google_project_service.run_api]
}
resource "google_cloud_run_service_iam_binding" "collector_server" {
location = google_cloud_run_v2_service.collector_server.location
service = google_cloud_run_v2_service.collector_server.name
role = "roles/run.invoker"
members = [
"allUsers"
]
}
# 3. Deploy Enrichment
locals {
config_enricher = base64encode(templatefile("${path.module}/configs/enricher/config.hocon.tmpl", {
project_id = var.project_id
enricher_input = google_pubsub_subscription.subscriptions["raw"].id
stream_enriched = google_pubsub_topic.topics["enriched"].name
stream_bad = google_pubsub_topic.topics["bad"].name
}))
enrichments = base64encode(templatefile("${path.module}/configs/enricher/enrichments.json.tmpl", { enrichments = join(",", local.enrichments_list) }))
}
resource "google_cloud_run_v2_job" "enrichment_job" {
name = "${var.prefix}-enrichment-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
image = "snowplow/snowplow-enrich-pubsub:latest-distroless"
args = [
"--config=${local.config_enricher}",
"--enrichments=${local.enrichments}",
"--iglu-config=${local.config_iglu_resolver}",
]
resources {
limits = {
cpu = "2"
memory = "1Gi"
}
}
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
}
# 4. Deploy BigQuery Loader
resource "google_bigquery_dataset" "bigquery_db" {
dataset_id = replace("${var.prefix}_snowplow", "-", "_")
location = var.region
labels = var.labels
}
resource "google_storage_bucket" "bq_loader_dead_letter_bucket" {
count = var.bigquery_loader_dead_letter_bucket_deploy ? 1 : 0
name = var.bigquery_loader_dead_letter_bucket_name
location = var.region
force_destroy = true
labels = var.labels
}
resource "google_storage_bucket_iam_binding" "dead_letter_storage_object_admin_binding" {
bucket = google_storage_bucket.bq_loader_dead_letter_bucket[0].name
role = "roles/storage.objectAdmin"
members = [google_service_account.cloud_run_sa.member]
}
resource "google_bigquery_dataset_iam_member" "dataset_bigquery_data_editor_binding" {
project = var.project_id
dataset_id = google_bigquery_dataset.bigquery_db.dataset_id
role = "roles/bigquery.dataEditor"
member = google_service_account.cloud_run_sa.member
}
locals {
bq_loader_dead_letter_bucket_name = coalesce(
join("", google_storage_bucket.bq_loader_dead_letter_bucket.*.name),
var.bigquery_loader_dead_letter_bucket_name,
)
config_loader = base64encode(templatefile("${path.module}/configs/loader/config.hocon.tmpl", {
project_id = var.project_id
loader_input = google_pubsub_subscription.subscriptions["enriched"].name
dataset_id = google_bigquery_dataset.bigquery_db.dataset_id
table_id = "events"
bad_topic = google_pubsub_topic.topics["bad"].name
failed_inserts_topic = google_pubsub_topic.topics["failed-inserts"].name
failed_inserts_sub = google_pubsub_subscription.subscriptions["failed-inserts"].name
mutator_types_topic = google_pubsub_topic.topics["loader-types"].name
mutator_types_sub = google_pubsub_subscription.subscriptions["loader-types"].name
dead_letter_bucket = google_storage_bucket.bq_loader_dead_letter_bucket[0].url
}))
}
# Mutator
resource "google_cloud_run_v2_job" "mutator_create_job" {
name = "${var.prefix}-mutator-create-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
image = "snowplow/snowplow-bigquery-mutator:latest-distroless"
args = [
"create",
"--config=${local.config_loader}",
"--resolver=${local.config_iglu_resolver}",
"--partitionColumn=collector_tstamp"
]
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
}
resource "google_cloud_run_v2_job" "mutator_listen_job" {
name = "${var.prefix}-mutator-listen-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
image = "snowplow/snowplow-bigquery-mutator:latest-distroless"
args = [
"listen",
"--config=${local.config_loader}",
"--resolver=${local.config_iglu_resolver}",
]
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
}
resource "google_cloud_run_v2_job" "streamloader" {
name = "${var.prefix}-streamloader-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
image = "snowplow/snowplow-bigquery-streamloader:latest"
args = [
"--config=${local.config_loader}",
"--resolver=${local.config_iglu_resolver}",
]
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
depends_on = [google_project_service.run_api]
}
resource "google_cloud_run_v2_job" "repeater" {
name = "${var.prefix}-repeater-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
image = "snowplow/snowplow-bigquery-repeater:latest-distroless"
args = [
"--config=${local.config_loader}",
"--resolver=${local.config_iglu_resolver}",
"--bufferSize=20",
"--timeout=20",
"--backoffPeriod=500"
]
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
depends_on = [google_project_service.run_api]
}
# dbt Job
locals {
dbt_run_script = base64encode(file("${path.module}/configs/dbt/run-dbt.sh"))
dbt_start_date = "${var.dbt_snowplow__start_date}"
dbt_snowplow__database = "${var.project_id}"
dbt_snowplow__atomic_schema = "${google_bigquery_dataset.bigquery_db.dataset_id}"
}
resource "google_cloud_run_v2_job" "dbt_job" {
name = "${var.prefix}-dbt-transform-job"
location = var.region
project = var.project_id
template {
template {
timeout = local.job_timeout
service_account = google_service_account.cloud_run_sa.email
containers {
# unfortunately GCP doesn't allow use to pull the dbt image from ghcr.io directly
# So we use a base python image and just install dbt-bigquery on it
image = "python:3.11"
env {
name = "BQ_DATASET"
value = "${google_bigquery_dataset.bigquery_db.dataset_id}"
}
env {
name = "BQ_LOCATION"
value = "${var.region}"
}
env {
name = "GOOGLE_PROJECT_ID"
value = "${var.project_id}"
}
command = [
"/bin/sh",
"-c",
"echo ${local.dbt_run_script} | base64 -d > run-dbt.sh && /bin/sh run-dbt.sh '${local.dbt_repo_url}' '${local.dbt_repo_folder_name}' '${local.dbt_snowplow__database}' '${local.dbt_snowplow__atomic_schema}' '${local.dbt_start_date}'"
]
}
max_retries = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
}
resource "google_cloud_scheduler_job" "jobs_scheduler" {
for_each = toset([ "repeater-job", "streamloader-job", "mutator-listen-job", "enrichment-job","dbt-transform-job"])
region = "europe-west1"
name = "${var.prefix}-${each.value}-scheduler"
description = "Trigger for ${each.value}"
schedule = "23 8-21/5 * * *"
time_zone = "Europe/Amsterdam"
attempt_deadline = "320s"
retry_config {
retry_count = 1
}
http_target {
http_method = "POST"
uri = "https://${var.region}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${var.project_id}/jobs/${var.prefix}-${each.value}:run"
oauth_token {
service_account_email = google_service_account.cloud_run_sa.email
scope = "https://www.googleapis.com/auth/cloud-platform"
}
}
}