-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
449 lines (358 loc) · 12.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
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
locals {
// Service account
service_account_name = lower(var.service_account_name)
service_account_email = "${local.service_account_name}@${var.project}.iam.gserviceaccount.com"
service_account_member = "serviceAccount:${local.service_account_email}"
// Storage
bucket_name = lower(var.bucket_name)
temp_folder_name = lower(var.temp_folder_name)
temp_location = "gs://${local.bucket_name}/${local.temp_folder_name}"
// Pub/Sub input
topic_name = lower(var.topic_name)
topic_schema_gcs_path = try(var.topic_schema.gcs_path, null)
subscription_name = lower(var.subscription_name)
// BigQuery success output
dataset_name = lower(var.dataset_name)
table_name = lower(var.table_name)
// Storage failure output
errors_bucket_name = lower(var.errors_bucket_name)
// BigQuery failure output
errors_dataset_name = lower(var.errors_dataset_name)
errors_table_name = lower(var.errors_table_name)
// Pub/Sub failure output
errors_topic_name = lower(var.errors_topic_name)
errors_subscription_name = lower(var.errors_subscription_name)
// Dataflow job(s)
job_name = lower(var.job_name)
jobs = { for job in var.jobs : job.version => job if !job.flex }
flex_jobs = { for job in var.jobs : job.version => job if job.flex }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Service Account for Dataflow Job
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
resource "google_service_account" "service_account" {
count = var.create_service_account ? 1 : 0
account_id = local.service_account_name
display_name = local.service_account_name
description = var.service_account_description
project = var.project
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Storage for Dataflow files
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create bucket
resource "google_storage_bucket" "bucket" {
count = var.create_bucket ? 1 : 0
name = local.bucket_name
project = var.project
location = var.location
labels = var.labels
uniform_bucket_level_access = true
}
// Assign object admin role on bucket
resource "google_storage_bucket_iam_member" "bucket_role" {
count = var.create_roles ? 1 : 0
bucket = local.bucket_name
role = "roles/storage.objectAdmin"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_storage_bucket.bucket
]
}
// Create folder for Dataflow temp files
resource "google_storage_bucket_object" "temp_folder" {
count = var.create_bucket ? 1 : 0
name = "${local.temp_folder_name}/"
bucket = local.bucket_name
content = "FOLDER"
depends_on = [google_storage_bucket.bucket]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Pub/Sub input
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create Pub/Sub topic schema
resource "google_pubsub_schema" "topic_schema" {
count = var.create_topic && var.topic_schema != null ? 1 : 0
name = local.topic_name
project = var.project
type = var.topic_schema.type
definition = var.topic_schema.definition
}
// Create Pub/Sub topic
resource "google_pubsub_topic" "topic" {
count = var.create_topic ? 1 : 0
name = local.topic_name
project = var.project
labels = var.labels
dynamic "schema_settings" {
for_each = var.topic_schema != null ? [var.topic_schema] : []
content {
schema = google_pubsub_schema.topic_schema[0].id
encoding = var.topic_schema.encoding
}
}
}
// Upload Pub/Sub topic schema to Storage
resource "google_storage_bucket_object" "topic_schema" {
count = local.topic_schema_gcs_path != null ? 1 : 0
name = local.topic_schema_gcs_path
bucket = local.bucket_name
content = var.topic_schema.definition
depends_on = [google_storage_bucket.bucket]
}
// Create Pub/Sub subscription
resource "google_pubsub_subscription" "subscription" {
count = var.create_subscription ? 1 : 0
name = local.subscription_name
topic = local.topic_name
project = var.project
labels = var.labels
ack_deadline_seconds = 60
retain_acked_messages = true
expiration_policy {
ttl = ""
}
depends_on = [google_pubsub_topic.topic]
}
// Assign subcriber and viewer roles on subscription
resource "google_pubsub_subscription_iam_member" "subscription_roles" {
for_each = var.create_roles ? toset(["subscriber", "viewer"]) : []
subscription = local.subscription_name
project = var.project
role = "roles/pubsub.${each.key}"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_pubsub_subscription.subscription
]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// BigQuery success output
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create dataset
resource "google_bigquery_dataset" "dataset" {
count = var.create_dataset ? 1 : 0
dataset_id = local.dataset_name
description = var.dataset_description
project = var.project
friendly_name = var.dataset_name
location = var.location
labels = var.labels
delete_contents_on_destroy = true
}
// Assign data editor role on dataset
resource "google_bigquery_dataset_iam_member" "dataset_role" {
count = var.create_roles ? 1 : 0
dataset_id = local.dataset_name
project = var.project
role = "roles/bigquery.dataEditor"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_bigquery_dataset.dataset
]
}
// Create table
resource "google_bigquery_table" "table" {
count = var.create_table ? 1 : 0
dataset_id = local.dataset_name
table_id = local.table_name
schema = var.table_schema
description = var.table_description
project = var.project
friendly_name = var.table_name
labels = var.labels
deletion_protection = false
time_partitioning {
type = "DAY"
}
depends_on = [google_bigquery_dataset.dataset]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Storage failure output
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create bucket for Dataflow errors
resource "google_storage_bucket" "errors_bucket" {
count = var.use_errors_bucket && var.create_errors_bucket ? 1 : 0
name = local.errors_bucket_name
project = var.project
location = var.location
labels = var.labels
uniform_bucket_level_access = true
}
// Assign object admin roles on bucket
resource "google_storage_bucket_iam_member" "errors_bucket_role" {
count = var.use_errors_bucket && var.create_roles ? 1 : 0
bucket = local.errors_bucket_name
role = "roles/storage.objectAdmin"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_storage_bucket.errors_bucket
]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// BigQuery failure output
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create dataset for Dataflow errors
resource "google_bigquery_dataset" "errors_dataset" {
count = var.use_errors_dataset && var.create_errors_dataset ? 1 : 0
dataset_id = local.errors_dataset_name
description = var.errors_dataset_description
project = var.project
friendly_name = var.errors_dataset_name
location = var.location
labels = var.labels
delete_contents_on_destroy = true
}
// Assign data editor role on dataset
resource "google_bigquery_dataset_iam_member" "errors_dataset_role" {
count = var.use_errors_dataset && var.create_roles ? 1 : 0
dataset_id = local.errors_dataset_name
project = var.project
role = "roles/bigquery.dataEditor"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_bigquery_dataset.errors_dataset
]
}
// Create table for Dataflow errors
resource "google_bigquery_table" "errors_table" {
count = var.use_errors_dataset && var.create_errors_table ? 1 : 0
dataset_id = local.errors_dataset_name
table_id = local.errors_table_name
description = var.errors_table_description
project = var.project
friendly_name = var.errors_table_name
labels = var.labels
deletion_protection = false
time_partitioning {
type = "DAY"
}
depends_on = [google_bigquery_dataset.errors_dataset]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Pub/Sub failure output
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create topic for Dataflow errors
resource "google_pubsub_topic" "errors_topic" {
count = var.use_errors_topic && var.create_errors_topic ? 1 : 0
name = local.errors_topic_name
project = var.project
labels = var.labels
}
// Assign publisher role on topic
resource "google_pubsub_topic_iam_member" "errors_topic_role" {
count = var.use_errors_topic && var.create_roles ? 1 : 0
topic = local.errors_topic_name
project = var.project
role = "roles/pubsub.publisher"
member = local.service_account_member
depends_on = [
google_service_account.service_account,
google_pubsub_topic.errors_topic
]
}
// Create subscription for Dataflow errors
resource "google_pubsub_subscription" "errors_subscription" {
count = var.use_errors_topic && var.create_errors_subscription ? 1 : 0
name = local.errors_subscription_name
topic = local.errors_topic_name
project = var.project
labels = var.labels
ack_deadline_seconds = 60
retain_acked_messages = true
expiration_policy {
ttl = ""
}
depends_on = [google_pubsub_topic.errors_topic]
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Dataflow Job(s)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Assign dataflow worker role on project
resource "google_project_iam_member" "dataflow_role" {
count = var.create_roles ? 1 : 0
project = var.project
role = "roles/dataflow.worker"
member = local.service_account_member
depends_on = [google_service_account.service_account]
}
// Create Dataflow job(s)
resource "google_dataflow_job" "jobs" {
for_each = local.jobs
name = "${local.job_name}-v${each.value.version}"
project = var.project
region = var.region
labels = var.labels
template_gcs_path = each.value.path
temp_gcs_location = local.temp_location
service_account_email = local.service_account_email
on_delete = "drain"
machine_type = var.machine_type
max_workers = var.max_workers
parameters = each.value.parameters
depends_on = [
google_service_account.service_account,
google_storage_bucket.bucket,
google_storage_bucket_iam_member.bucket_role,
google_storage_bucket_object.temp_folder,
google_storage_bucket_object.topic_schema,
google_pubsub_topic.topic,
google_pubsub_subscription.subscription,
google_pubsub_subscription_iam_member.subscription_roles,
google_bigquery_dataset.dataset,
google_bigquery_dataset_iam_member.dataset_role,
google_bigquery_table.table,
google_storage_bucket.errors_bucket,
google_storage_bucket_iam_member.errors_bucket_role,
google_bigquery_dataset.errors_dataset,
google_bigquery_dataset_iam_member.errors_dataset_role,
google_bigquery_table.errors_table,
google_pubsub_topic.errors_topic,
google_pubsub_topic_iam_member.errors_topic_role,
google_pubsub_subscription.errors_subscription,
google_project_iam_member.dataflow_role
]
}
// Create Dataflow Flex job(s)
resource "google_dataflow_flex_template_job" "flex_jobs" {
provider = google-beta
for_each = local.flex_jobs
name = "${local.job_name}-v${each.value.version}"
project = var.project
region = var.region
labels = var.labels
container_spec_gcs_path = each.value.path
on_delete = "drain"
parameters = merge({
workerMachineType = var.machine_type
maxNumWorkers = var.max_workers
serviceAccount = local.service_account_email
tempLocation = local.temp_location
}, each.value.parameters)
depends_on = [
google_service_account.service_account,
google_storage_bucket.bucket,
google_storage_bucket_iam_member.bucket_role,
google_storage_bucket_object.temp_folder,
google_storage_bucket_object.topic_schema,
google_pubsub_topic.topic,
google_pubsub_subscription.subscription,
google_pubsub_subscription_iam_member.subscription_roles,
google_bigquery_dataset.dataset,
google_bigquery_dataset_iam_member.dataset_role,
google_bigquery_table.table,
google_storage_bucket.errors_bucket,
google_storage_bucket_iam_member.errors_bucket_role,
google_bigquery_dataset.errors_dataset,
google_bigquery_dataset_iam_member.errors_dataset_role,
google_bigquery_table.errors_table,
google_pubsub_topic.errors_topic,
google_pubsub_topic_iam_member.errors_topic_role,
google_pubsub_subscription.errors_subscription,
google_project_iam_member.dataflow_role
]
}