This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
323 lines (266 loc) · 9.54 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
locals {
module_name = "snowplow-bigquery-loader-apps"
module_version = "0.1.0"
app_name = "bigquery-loader-apps"
app_version = "0.2.0"
local_labels = {
module_name = local.module_name
module_version = replace(local.module_version, ".", "-")
}
labels = merge(
var.labels,
local.local_labels
)
ssh_keys_metadata = <<EOF
%{for v in var.ssh_key_pairs~}
${v.user_name}:${v.public_key}
%{endfor~}
EOF
}
module "telemetry" {
source = "snowplow-devops/telemetry/snowplow"
version = "0.2.0"
count = var.telemetry_enabled ? 1 : 0
user_provided_id = var.user_provided_id
cloud = "GCP"
region = var.region
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
# --- CE: Boot Image
data "google_compute_image" "ubuntu_20_04" {
family = "ubuntu-2004-lts"
project = "ubuntu-os-cloud"
}
# --- GCS: Set Up Google Cloud Storage
resource "google_storage_bucket" "dead_letter" {
name = "${var.name}-bq-loader-dead-letter"
location = var.region
force_destroy = true
versioning {
enabled = false
}
}
# --- BigQuery Dataset
resource "google_bigquery_dataset" "snowplow" {
count = var.dataset_config.create == true ? 1 : 0
dataset_id = var.dataset_config.name
}
# --- IAM: Service Account Setup
resource "google_service_account" "sa" {
account_id = var.name
display_name = "Snowplow BigQuery Loader service account - ${var.name}"
}
resource "google_project_iam_member" "sa_pubsub_viewer" {
project = var.project_id
role = "roles/pubsub.viewer"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_pubsub_publisher" {
project = var.project_id
role = "roles/pubsub.publisher"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_pubsub_subscriber" {
project = var.project_id
role = "roles/pubsub.subscriber"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_logging_log_writer" {
project = var.project_id
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_storage_object_viewer" {
role = "roles/storage.objectViewer"
member = "serviceAccount:${google_service_account.sa.email}"
project = var.project_id
}
resource "google_storage_bucket_iam_binding" "binding" {
bucket = google_storage_bucket.dead_letter.name
role = "roles/storage.objectAdmin"
members = [
"serviceAccount:${google_service_account.sa.email}"
]
}
locals {
created_or_provided_dataset_id = var.dataset_config.create ? regex("[^/]+$", google_bigquery_dataset.snowplow[0].id) : var.dataset_config.name
created_or_provided_table_id = var.table_config.name
}
resource "google_bigquery_dataset_iam_member" "sa_bigquery_dataset_editor" {
project = var.project_id
dataset_id = local.created_or_provided_dataset_id
role = "roles/bigquery.dataEditor"
member = "serviceAccount:${google_service_account.sa.email}"
depends_on = [
google_bigquery_dataset.snowplow
]
}
# --- PubSub: Set Up Subscriptions and Topics
resource "google_pubsub_topic" "types_topic" {
name = "${var.name}-types-topic"
labels = local.labels
}
resource "google_pubsub_topic" "bad_types_topic" {
name = "${var.name}-bad-types-topic"
labels = local.labels
}
resource "google_pubsub_topic" "failed_insert_topic" {
name = "${var.name}-failed-insert-topic"
labels = local.labels
}
resource "google_pubsub_subscription" "input_subscription" {
name = "${var.name}-input-subscription"
topic = var.enriched_topic_id
labels = local.labels
message_retention_duration = "1200s"
retain_acked_messages = true
ack_deadline_seconds = 20
enable_message_ordering = false
expiration_policy {
ttl = "300000.5s"
}
retry_policy {
minimum_backoff = "10s"
}
}
resource "google_pubsub_subscription" "types_subscription" {
name = "${var.name}-types-subscription"
topic = google_pubsub_topic.types_topic.id
labels = local.labels
message_retention_duration = "1200s"
retain_acked_messages = true
ack_deadline_seconds = 20
enable_message_ordering = false
expiration_policy {
ttl = "300000.5s"
}
retry_policy {
minimum_backoff = "10s"
}
}
resource "google_pubsub_subscription" "failed_insert_subscription" {
name = "${var.name}-failed-insert-subscription"
topic = google_pubsub_topic.failed_insert_topic.id
labels = local.labels
message_retention_duration = "1200s"
retain_acked_messages = true
ack_deadline_seconds = 20
enable_message_ordering = false
expiration_policy {
ttl = "300000.5s"
}
retry_policy {
minimum_backoff = "10s"
}
}
# --- CE: Create Compute Instance Template
resource "google_compute_instance_template" "tpl" {
name_prefix = local.module_name
description = "This template is used to create Compute Engine instances, running the Snowplow BigQuery Loader apps."
instance_description = var.name
machine_type = var.machine_type
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
}
disk {
source_image = var.ubuntu_20_04_source_image == "" ? data.google_compute_image.ubuntu_20_04.self_link : var.ubuntu_20_04_source_image
auto_delete = true
boot = true
disk_type = "pd-standard"
disk_size_gb = 10
}
# Note: Only one of either network or subnetwork can be supplied
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance_template#network_interface
network_interface {
network = var.subnetwork == "" ? var.network : ""
subnetwork = var.subnetwork
dynamic "access_config" {
for_each = var.associate_public_ip_address ? [1] : []
content {
network_tier = "PREMIUM"
}
}
}
service_account {
email = google_service_account.sa.email
scopes = ["cloud-platform"]
}
metadata = {
block-project-ssh-keys = var.ssh_block_project_keys
ssh-keys = local.ssh_keys_metadata
}
tags = [var.name]
labels = local.labels
lifecycle {
create_before_destroy = true
}
}
# --- Populate Terraform Templates to Use in Subsequent Steps
locals {
enriched_topic_name = regex("[^/]+$", var.enriched_topic_id)
types_topic_name = regex("[^/]+$", google_pubsub_topic.types_topic.id)
bad_types_topic_name = regex("[^/]+$", google_pubsub_topic.bad_types_topic.id)
failed_inserts_topic_name = regex("[^/]+$", google_pubsub_topic.failed_insert_topic.id)
input_sub_name = regex("[^/]+$", google_pubsub_subscription.input_subscription.id)
types_sub_name = regex("[^/]+$", google_pubsub_subscription.types_subscription.id)
failed_inserts_sub_name = regex("[^/]+$", google_pubsub_subscription.failed_insert_subscription.id)
shared_hocon = templatefile("${path.module}/templates/config.hocon.tmpl", {
dataset_id = local.created_or_provided_dataset_id
table_id = local.created_or_provided_table_id
types_topic = local.types_topic_name
bad_types_topic = local.bad_types_topic_name
failed_inserts_topic = local.failed_inserts_topic_name
input_sub = local.input_sub_name
types_sub = local.types_sub_name
failed_inserts_sub = local.failed_inserts_sub_name
dead_letter_bucket_path = google_storage_bucket.dead_letter.url
project_id = var.project_id
})
resolver = file("${path.module}/templates/resolver.json.tmpl")
images_by_name = {
for i in var.images : regex("snowplow[/]([a-z-]*):", i)[0] => {
image = i
template_path = "${path.module}/templates/${regex("snowplow[/]([a-z-]*):", i)[0]}.sh.tmpl"
metadata_startup_script = templatefile("${path.module}/templates/startup-script.sh.tmpl", {
telemetry_script = join("", module.telemetry.*.gcp_ubuntu_20_04_user_data)
config_hocon_contents = local.shared_hocon
resolver_json_contents = local.resolver
telemetry_enabled = var.telemetry_enabled
image_script = templatefile("${path.module}/templates/${regex("snowplow[/]([a-z-]*):", i)[0]}.sh.tmpl", {
name = i
image = i
gcp_logs_enabled = var.gcp_logs_enabled
})
})
}
}
}
# --- Local: Save Compiled config.hocon and resolver.json
resource "local_file" "config" {
content = local.shared_hocon
filename = "${path.module}/templates/compiled/config.hocon"
}
resource "local_file" "resolver" {
content = local.resolver
filename = "${path.module}/templates/compiled/resolver.json"
}
resource "local_file" "startup_scripts" {
for_each = local.images_by_name
filename = "${path.module}/templates/compiled/${each.key}-startup-script.sh"
content = each.value.metadata_startup_script
}
# --- CE: Create Apps from Compute Instance Template
resource "google_compute_instance_from_template" "snowplow_bq_app" {
for_each = local.images_by_name
name = "${var.name}-${each.key}"
zone = var.zone
source_instance_template = google_compute_instance_template.tpl.id
metadata_startup_script = each.value.metadata_startup_script
depends_on = [
google_bigquery_dataset_iam_member.sa_bigquery_dataset_editor
]
}