-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.tf
More file actions
1484 lines (1363 loc) · 64.4 KB
/
main.tf
File metadata and controls
1484 lines (1363 loc) · 64.4 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.34.0"
}
}
}
#
# MobilityData 2023
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
locals {
x_number_of_concurrent_instance = 4
deployment_timestamp = formatdate("YYYYMMDDhhmmss", timestamp())
function_tokens_config = jsondecode(file("${path.module}../../../functions-python/tokens/function_config.json"))
function_tokens_zip = "${path.module}/../../functions-python/tokens/.dist/tokens.zip"
# DEV and QA use the vpc connector
vpc_connector_name = lower(var.environment) == "dev" ? "vpc-connector-qa" : "vpc-connector-${lower(var.environment)}"
vpc_connector_project = lower(var.environment) == "dev" ? "mobility-feeds-qa" : var.project_id
function_process_validation_report_config = jsondecode(file("${path.module}/../../functions-python/process_validation_report/function_config.json"))
function_process_validation_report_zip = "${path.module}/../../functions-python/process_validation_report/.dist/process_validation_report.zip"
public_hosted_datasets_url = lower(var.environment) == "prod" ? "https://${var.public_hosted_datasets_dns}" : "https://${var.environment}-${var.public_hosted_datasets_dns}"
function_update_validation_report_config = jsondecode(file("${path.module}/../../functions-python/update_validation_report/function_config.json"))
function_update_validation_report_zip = "${path.module}/../../functions-python/update_validation_report/.dist/update_validation_report.zip"
function_gbfs_validation_report_config = jsondecode(file("${path.module}/../../functions-python/gbfs_validator/function_config.json"))
function_gbfs_validation_report_zip = "${path.module}/../../functions-python/gbfs_validator/.dist/gbfs_validator.zip"
function_reverse_geolocation_populate_config = jsondecode(file("${path.module}/../../functions-python/reverse_geolocation_populate/function_config.json"))
function_reverse_geolocation_populate_zip = "${path.module}/../../functions-python/reverse_geolocation_populate/.dist/reverse_geolocation_populate.zip"
function_feed_sync_dispatcher_transitland_config = jsondecode(file("${path.module}/../../functions-python/feed_sync_dispatcher_transitland/function_config.json"))
function_feed_sync_dispatcher_transitland_zip = "${path.module}/../../functions-python/feed_sync_dispatcher_transitland/.dist/feed_sync_dispatcher_transitland.zip"
function_feed_sync_process_transitland_config = jsondecode(file("${path.module}/../../functions-python/feed_sync_process_transitland/function_config.json"))
function_feed_sync_process_transitland_zip = "${path.module}/../../functions-python/feed_sync_process_transitland/.dist/feed_sync_process_transitland.zip"
function_operations_api_config = jsondecode(file("${path.module}/../../functions-python/operations_api/function_config.json"))
function_operations_api_zip = "${path.module}/../../functions-python/operations_api/.dist/operations_api.zip"
function_backfill_dataset_service_date_range_config = jsondecode(file("${path.module}/../../functions-python/backfill_dataset_service_date_range/function_config.json"))
function_backfill_dataset_service_date_range_zip = "${path.module}/../../functions-python/backfill_dataset_service_date_range/.dist/backfill_dataset_service_date_range.zip"
function_reverse_geolocation_config = jsondecode(file("${path.module}/../../functions-python/reverse_geolocation/function_config.json"))
function_reverse_geolocation_zip = "${path.module}/../../functions-python/reverse_geolocation/.dist/reverse_geolocation.zip"
function_update_feed_status_config = jsondecode(file("${path.module}/../../functions-python/update_feed_status/function_config.json"))
function_update_feed_status_zip = "${path.module}/../../functions-python/update_feed_status/.dist/update_feed_status.zip"
function_export_csv_config = jsondecode(file("${path.module}/../../functions-python/export_csv/function_config.json"))
function_export_csv_zip = "${path.module}/../../functions-python/export_csv/.dist/export_csv.zip"
function_tasks_executor_config = jsondecode(file("${path.module}/../../functions-python/tasks_executor/function_config.json"))
function_tasks_executor_zip = "${path.module}/../../functions-python/tasks_executor/.dist/tasks_executor.zip"
}
locals {
# To allow multiple functions to access the same secrets, we need to combine all the keys from the different functions
# Combine all keys into a list
all_secret_keys_list = concat(
[for x in local.function_tokens_config.secret_environment_variables : x.key],
[for x in local.function_process_validation_report_config.secret_environment_variables : x.key],
[for x in local.function_gbfs_validation_report_config.secret_environment_variables : x.key],
[for x in local.function_update_validation_report_config.secret_environment_variables : x.key],
[for x in local.function_backfill_dataset_service_date_range_config.secret_environment_variables : x.key],
[for x in local.function_update_feed_status_config.secret_environment_variables : x.key],
[for x in local.function_export_csv_config.secret_environment_variables : x.key],
[for x in local.function_tasks_executor_config.secret_environment_variables : x.key]
)
# Convert the list to a set to ensure uniqueness
unique_secret_keys = toset(local.all_secret_keys_list)
}
data "google_vpc_access_connector" "vpc_connector" {
name = local.vpc_connector_name
region = var.gcp_region
project = local.vpc_connector_project
}
data "google_pubsub_topic" "datasets_batch_topic" {
name = "datasets-batch-topic-${var.environment}"
}
data "google_storage_bucket" "datasets_bucket" {
name = "${var.datasets_bucket_name}-${var.environment}"
}
# Service account to execute the cloud functions
resource "google_service_account" "functions_service_account" {
account_id = "functions-service-account"
display_name = "Functions Service Account"
}
resource "google_storage_bucket" "functions_bucket" {
name = "mobility-feeds-functions-python-${var.environment}"
location = "us"
}
resource "google_storage_bucket" "gbfs_snapshots_bucket" {
location = var.gcp_region
name = "${var.gbfs_bucket_name}-${var.environment}"
cors {
origin = ["*"]
method = ["GET"]
response_header = ["*"]
}
}
resource "google_storage_bucket_iam_member" "datasets_bucket_functions_service_account" {
bucket = data.google_storage_bucket.datasets_bucket.name
role = "roles/storage.admin"
member = "serviceAccount:${google_service_account.functions_service_account.email}"
}
resource "google_project_iam_member" "datasets_bucket_functions_service_account" {
project = var.project_id
member = "serviceAccount:${google_service_account.functions_service_account.email}"
role = "roles/storage.admin"
}
# Cloud function source code zip files:
# 1. Tokens
resource "google_storage_bucket_object" "function_token_zip" {
name = "tokens-${substr(filebase64sha256(local.function_tokens_zip),0,10)}.zip"
bucket = google_storage_bucket.functions_bucket.name
source = local.function_tokens_zip
}
# 3. Process validation report
resource "google_storage_bucket_object" "process_validation_report_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "process-validation-report-${substr(filebase64sha256(local.function_process_validation_report_zip), 0, 10)}.zip"
source = local.function_process_validation_report_zip
}
# 4. Update validation report
resource "google_storage_bucket_object" "update_validation_report_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "update-validation-report-${substr(filebase64sha256(local.function_update_validation_report_zip), 0, 10)}.zip"
source = local.function_update_validation_report_zip
}
# 5. GBFS validation report
resource "google_storage_bucket_object" "gbfs_validation_report_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "gbfs-validator-${substr(filebase64sha256(local.function_gbfs_validation_report_zip), 0, 10)}.zip"
source = local.function_gbfs_validation_report_zip
}
# 6. Feed sync dispatcher transitland
resource "google_storage_bucket_object" "feed_sync_dispatcher_transitland_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "feed-sync-dispatcher-transitland-${substr(filebase64sha256(local.function_feed_sync_dispatcher_transitland_zip), 0, 10)}.zip"
source = local.function_feed_sync_dispatcher_transitland_zip
}
# 7. Feed sync process transitland
resource "google_storage_bucket_object" "feed_sync_process_transitland_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "feed-sync-process-transitland-${substr(filebase64sha256(local.function_feed_sync_process_transitland_zip), 0, 10)}.zip"
source = local.function_feed_sync_process_transitland_zip
}
# 8. Operations API
resource "google_storage_bucket_object" "operations_api_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "operations-api-${substr(filebase64sha256(local.function_operations_api_zip), 0, 10)}.zip"
source = local.function_operations_api_zip
}
# 9. Backfill Gtfs Datasets Service Date Range
resource "google_storage_bucket_object" "backfill_dataset_service_date_range_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "backfill-dataset-service-date-range-${substr(filebase64sha256(local.function_backfill_dataset_service_date_range_zip), 0, 10)}.zip"
source = local.function_backfill_dataset_service_date_range_zip
}
# 10. Export CSV
resource "google_storage_bucket_object" "export_csv_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "export-csv-${substr(filebase64sha256(local.function_export_csv_zip), 0, 10)}.zip"
source = local.function_export_csv_zip
}
# 11. Update Feed Status
resource "google_storage_bucket_object" "update_feed_status_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "backfill-dataset-service-date-range-${substr(filebase64sha256(local.function_update_feed_status_zip), 0, 10)}.zip"
source = local.function_update_feed_status_zip
}
# 12. Reverse geolocation populate
resource "google_storage_bucket_object" "reverse_geolocation_populate_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "reverse-geolocation-populate-${substr(filebase64sha256(local.function_reverse_geolocation_populate_zip), 0, 10)}.zip"
source = local.function_reverse_geolocation_populate_zip
}
# 13. Reverse geolocation
resource "google_storage_bucket_object" "reverse_geolocation_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "reverse-geolocation-${substr(filebase64sha256(local.function_reverse_geolocation_zip), 0, 10)}.zip"
source = local.function_reverse_geolocation_zip
}
# 14. Task Executor
resource "google_storage_bucket_object" "tasks_executor_zip" {
bucket = google_storage_bucket.functions_bucket.name
name = "task-executor-${substr(filebase64sha256(local.function_tasks_executor_zip), 0, 10)}.zip"
source = local.function_tasks_executor_zip
}
# Secrets access
resource "google_secret_manager_secret_iam_member" "secret_iam_member" {
for_each = local.unique_secret_keys
project = var.project_id
# The secret_id is the current item in the set. Since these are unique keys, we use each.value to access it.
secret_id = "${upper(var.environment)}_${each.value}"
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:${google_service_account.functions_service_account.email}"
}
# Cloud function definitions
# 1. functions-python/tokens cloud function
resource "google_cloudfunctions2_function" "tokens" {
name = local.function_tokens_config.name
description = local.function_tokens_config.description
location = var.gcp_region
build_config {
runtime = var.python_runtime
entry_point = local.function_tokens_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.function_token_zip.name
}
}
}
service_config {
available_memory = local.function_tokens_config.memory
timeout_seconds = local.function_tokens_config.timeout
available_cpu = local.function_tokens_config.available_cpu
max_instance_request_concurrency = local.function_tokens_config.max_instance_request_concurrency
max_instance_count = local.function_tokens_config.max_instance_count
min_instance_count = local.function_tokens_config.min_instance_count
dynamic "secret_environment_variables" {
for_each = local.function_tokens_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
ingress_settings = local.function_tokens_config.ingress_settings
}
}
# 3. functions/validation_report_processor
# 3.1 functions/validation_report_processor cloud function
# Create a queue for the cloud tasks
# The 2X rate is defined as 4*2 concurrent dispatches and 1 dispatch per second
# The name of the queue need to be dynamic due to GCP limitations
# references:
# - https://cloud.google.com/tasks/docs/deleting-appengine-queues-and-tasks#deleting_queues
# - https://issuetracker.google.com/issues/263947953
resource "google_cloud_tasks_queue" "cloud_tasks_2x_rate_queue" {
name = "cloud-tasks-2x-rate-queue-${var.environment}-${local.deployment_timestamp}"
location = var.gcp_region
rate_limits {
max_concurrent_dispatches = local.x_number_of_concurrent_instance * 2
max_dispatches_per_second = 1
}
retry_config {
# This will make the cloud task retry for ~two hours
max_attempts = 120
min_backoff = "20s"
max_backoff = "60s"
max_doublings = 2
}
}
output "processing_report_cloud_task_name" {
value = google_cloud_tasks_queue.cloud_tasks_2x_rate_queue.name
}
resource "google_cloudfunctions2_function" "process_validation_report" {
name = local.function_process_validation_report_config.name
description = local.function_process_validation_report_config.description
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
project = var.project_id
build_config {
runtime = var.python_runtime
entry_point = local.function_process_validation_report_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.process_validation_report_zip.name
}
}
}
service_config {
available_memory = local.function_process_validation_report_config.memory
available_cpu = local.function_process_validation_report_config.available_cpu
timeout_seconds = local.function_process_validation_report_config.timeout
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
FILES_ENDPOINT = local.public_hosted_datasets_url
# prevents multiline logs from being truncated on GCP console
PYTHONNODEBUGRANGES = 0
}
dynamic "secret_environment_variables" {
for_each = local.function_process_validation_report_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = lookup(secret_environment_variables.value, "secret", "${upper(var.environment)}_${secret_environment_variables.value["key"]}")
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
max_instance_request_concurrency = local.function_process_validation_report_config.max_instance_request_concurrency
max_instance_count = local.function_process_validation_report_config.max_instance_count
min_instance_count = local.function_process_validation_report_config.min_instance_count
}
}
# 3.2 functions/compute_validation_report_counters cloud function
resource "google_cloudfunctions2_function" "compute_validation_report_counters" {
name = "compute-validation-report-counters"
description = "Cloud function to compute counters for validation reports"
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
project = var.project_id
build_config {
runtime = var.python_runtime
entry_point = "compute_validation_report_counters"
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.process_validation_report_zip.name
}
}
}
service_config {
available_memory = "512Mi"
available_cpu = "1"
timeout_seconds = 300
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
ENV = var.environment
PYTHONNODEBUGRANGES = 0
}
dynamic "secret_environment_variables" {
for_each = local.function_process_validation_report_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = lookup(secret_environment_variables.value, "secret", "${upper(var.environment)}_${secret_environment_variables.value["key"]}")
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
max_instance_request_concurrency = 1
max_instance_count = 1
min_instance_count = 0
}
}
# 4. functions/update_validation_report cloud function
resource "google_cloudfunctions2_function" "update_validation_report" {
location = var.gcp_region
name = local.function_update_validation_report_config.name
description = local.function_update_validation_report_config.description
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
project = var.project_id
build_config {
runtime = var.python_runtime
entry_point = local.function_update_validation_report_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.update_validation_report_zip.name
}
}
}
service_config {
available_memory = local.function_update_validation_report_config.memory
available_cpu = local.function_update_validation_report_config.available_cpu
timeout_seconds = local.function_update_validation_report_config.timeout
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
ENV = var.environment
MAX_RETRY = 10
BATCH_SIZE = 5
WEB_VALIDATOR_URL = var.validator_endpoint
# prevents multiline logs from being truncated on GCP console
PYTHONNODEBUGRANGES = 0
}
dynamic "secret_environment_variables" {
for_each = local.function_update_validation_report_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = lookup(secret_environment_variables.value, "secret", "${upper(var.environment)}_${secret_environment_variables.value["key"]}")
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
max_instance_request_concurrency = local.function_update_validation_report_config.max_instance_request_concurrency
max_instance_count = local.function_update_validation_report_config.max_instance_count
min_instance_count = local.function_update_validation_report_config.min_instance_count
}
}
# 5. functions/gbfs_validator cloud function
# 5.1 Create Pub/Sub topic
resource "google_pubsub_topic" "validate_gbfs_feed" {
name = "validate-gbfs-feed"
}
# 5.2 Create batch function that publishes to the Pub/Sub topic
resource "google_cloudfunctions2_function" "gbfs_validator_batch" {
name = "${local.function_gbfs_validation_report_config.name}-batch"
description = local.function_gbfs_validation_report_config.description
location = var.gcp_region
depends_on = [google_project_iam_member.event-receiving, google_secret_manager_secret_iam_member.secret_iam_member]
build_config {
runtime = var.python_runtime
entry_point = "${local.function_gbfs_validation_report_config.entry_point}_batch"
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.gbfs_validation_report_zip.name
}
}
}
service_config {
environment_variables = {
PROJECT_ID = var.project_id
PUBSUB_TOPIC_NAME = google_pubsub_topic.validate_gbfs_feed.name
PYTHONNODEBUGRANGES = 0
}
available_memory = "1Gi"
timeout_seconds = local.function_gbfs_validation_report_config.timeout
available_cpu = local.function_gbfs_validation_report_config.available_cpu
max_instance_request_concurrency = local.function_gbfs_validation_report_config.max_instance_request_concurrency
max_instance_count = local.function_gbfs_validation_report_config.max_instance_count
min_instance_count = local.function_gbfs_validation_report_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = "ALLOW_ALL"
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
dynamic "secret_environment_variables" {
for_each = local.function_gbfs_validation_report_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# Schedule the batch function to run
resource "google_cloud_scheduler_job" "gbfs_validator_batch_scheduler" {
name = "gbfs-validator-batch-scheduler-${var.environment}"
description = "Schedule the gbfs-validator-batch function"
time_zone = "Etc/UTC"
schedule = var.gbfs_scheduler_schedule
region = var.gcp_region
paused = var.environment == "prod" ? false : true
depends_on = [google_cloudfunctions2_function.gbfs_validator_batch, google_cloudfunctions2_function_iam_member.gbfs_validator_batch_invoker]
http_target {
http_method = "POST"
uri = google_cloudfunctions2_function.gbfs_validator_batch.url
oidc_token {
service_account_email = google_service_account.functions_service_account.email
}
headers = {
"Content-Type" = "application/json"
}
}
attempt_deadline = "320s"
}
resource "google_cloud_scheduler_job" "transit_land_scraping_scheduler" {
name = "transitland-scraping-scheduler-${var.environment}"
description = "Schedule the transitland scraping function"
time_zone = "Etc/UTC"
schedule = var.transitland_scraping_schedule
region = var.gcp_region
paused = var.environment == "prod" ? false : true
depends_on = [google_cloudfunctions2_function.feed_sync_dispatcher_transitland, google_cloudfunctions2_function_iam_member.transitland_feeds_dispatcher_invoker]
http_target {
http_method = "POST"
uri = google_cloudfunctions2_function.feed_sync_dispatcher_transitland.url
oidc_token {
service_account_email = google_service_account.functions_service_account.email
}
headers = {
"Content-Type" = "application/json"
}
}
attempt_deadline = "320s"
}
# 5.3 Create function that subscribes to the Pub/Sub topic
resource "google_cloudfunctions2_function" "gbfs_validator_pubsub" {
name = "${local.function_gbfs_validation_report_config.name}-pubsub"
description = local.function_gbfs_validation_report_config.description
location = var.gcp_region
depends_on = [google_project_iam_member.event-receiving, google_secret_manager_secret_iam_member.secret_iam_member]
event_trigger {
trigger_region = var.gcp_region
service_account_email = google_service_account.functions_service_account.email
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.validate_gbfs_feed.id
retry_policy = "RETRY_POLICY_RETRY"
}
build_config {
runtime = var.python_runtime
entry_point = "${local.function_gbfs_validation_report_config.entry_point}_pubsub"
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.gbfs_validation_report_zip.name
}
}
}
service_config {
available_memory = local.function_gbfs_validation_report_config.memory
timeout_seconds = local.function_gbfs_validation_report_config.timeout
available_cpu = local.function_gbfs_validation_report_config.available_cpu
max_instance_request_concurrency = local.function_gbfs_validation_report_config.max_instance_request_concurrency
max_instance_count = local.function_gbfs_validation_report_config.max_instance_count
min_instance_count = local.function_gbfs_validation_report_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = "ALLOW_ALL"
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
ENV = var.environment
BUCKET_NAME = google_storage_bucket.gbfs_snapshots_bucket.name
PROJECT_ID = var.project_id
GCP_REGION = var.gcp_region
SERVICE_ACCOUNT_EMAIL = google_service_account.functions_service_account.email
QUEUE_NAME = google_cloud_tasks_queue.reverse_geolocation_task_queue_processor.name
}
dynamic "secret_environment_variables" {
for_each = local.function_gbfs_validation_report_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 6. functions/feed_sync_dispatcher_transitland cloud function
# 6.1 Create Pub/Sub topic
resource "google_pubsub_topic" "transitland_feeds_dispatch" {
name = "transitland-feeds-dispatch"
}
# 6.2 Create batch function that publishes to the Pub/Sub topic
resource "google_cloudfunctions2_function" "feed_sync_dispatcher_transitland" {
name = "${local.function_feed_sync_dispatcher_transitland_config.name}-batch"
description = local.function_feed_sync_dispatcher_transitland_config.description
location = var.gcp_region
depends_on = [google_project_iam_member.event-receiving, google_secret_manager_secret_iam_member.secret_iam_member]
build_config {
runtime = var.python_runtime
entry_point = local.function_feed_sync_dispatcher_transitland_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.feed_sync_dispatcher_transitland_zip.name
}
}
}
service_config {
environment_variables = {
PROJECT_ID = var.project_id
PYTHONNODEBUGRANGES = 0
PUBSUB_TOPIC_NAME = google_pubsub_topic.transitland_feeds_dispatch.name
TRANSITLAND_API_KEY=var.transitland_api_key
TRANSITLAND_OPERATOR_URL="https://transit.land/api/v2/rest/operators"
TRANSITLAND_FEED_URL="https://transit.land/api/v2/rest/feeds"
}
available_memory = local.function_feed_sync_dispatcher_transitland_config.available_memory
timeout_seconds = local.function_feed_sync_dispatcher_transitland_config.timeout
available_cpu = local.function_feed_sync_dispatcher_transitland_config.available_cpu
max_instance_request_concurrency = local.function_feed_sync_dispatcher_transitland_config.max_instance_request_concurrency
max_instance_count = local.function_feed_sync_dispatcher_transitland_config.max_instance_count
min_instance_count = local.function_feed_sync_dispatcher_transitland_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = local.function_feed_sync_dispatcher_transitland_config.ingress_settings
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
dynamic "secret_environment_variables" {
for_each = local.function_feed_sync_dispatcher_transitland_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 7. functions/operations_api cloud function
resource "google_cloudfunctions2_function" "operations_api" {
name = "${local.function_operations_api_config.name}"
description = local.function_operations_api_config.description
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
build_config {
runtime = var.python_runtime
entry_point = local.function_operations_api_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.operations_api_zip.name
}
}
}
service_config {
environment_variables = {
PROJECT_ID = var.project_id
PYTHONNODEBUGRANGES = 0
GOOGLE_CLIENT_ID = var.operations_oauth2_client_id
}
available_memory = local.function_operations_api_config.memory
timeout_seconds = local.function_operations_api_config.timeout
available_cpu = local.function_operations_api_config.available_cpu
max_instance_request_concurrency = local.function_operations_api_config.max_instance_request_concurrency
max_instance_count = local.function_operations_api_config.max_instance_count
min_instance_count = local.function_operations_api_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = local.function_operations_api_config.ingress_settings
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
dynamic "secret_environment_variables" {
for_each = local.function_operations_api_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 8. functions/feed_sync_process_transitland cloud function
resource "google_cloudfunctions2_function" "feed_sync_process_transitland" {
name = "${local.function_feed_sync_process_transitland_config.name}-pubsub"
description = local.function_feed_sync_process_transitland_config.description
location = var.gcp_region
depends_on = [google_project_iam_member.event-receiving, google_secret_manager_secret_iam_member.secret_iam_member]
event_trigger {
trigger_region = var.gcp_region
service_account_email = google_service_account.functions_service_account.email
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.transitland_feeds_dispatch.id
retry_policy = "RETRY_POLICY_RETRY"
}
build_config {
runtime = var.python_runtime
entry_point = local.function_feed_sync_process_transitland_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.feed_sync_process_transitland_zip.name
}
}
}
service_config {
available_memory = local.function_feed_sync_process_transitland_config.memory
timeout_seconds = local.function_feed_sync_process_transitland_config.timeout
available_cpu = local.function_feed_sync_process_transitland_config.available_cpu
max_instance_request_concurrency = local.function_feed_sync_process_transitland_config.max_instance_request_concurrency
max_instance_count = local.function_feed_sync_process_transitland_config.max_instance_count
min_instance_count = local.function_feed_sync_process_transitland_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = var.environment == "dev" ? "ALLOW_ALL" : local.function_feed_sync_process_transitland_config.ingress_settings
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
PYTHONNODEBUGRANGES = 0
DB_REUSE_SESSION = "True"
PROJECT_ID = var.project_id
PUBSUB_TOPIC_NAME = google_pubsub_topic.transitland_feeds_dispatch.name
DATASET_BATCH_TOPIC_NAME = data.google_pubsub_topic.datasets_batch_topic.name
}
dynamic "secret_environment_variables" {
for_each = local.function_feed_sync_process_transitland_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 9. functions/backfill_dataset_service_date_range cloud function
# Fills all the NULL values for service date range in the gtfs datasets table
resource "google_cloudfunctions2_function" "backfill_dataset_service_date_range" {
name = local.function_backfill_dataset_service_date_range_config.name
description = local.function_backfill_dataset_service_date_range_config.description
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
project = var.project_id
build_config {
runtime = var.python_runtime
entry_point = local.function_backfill_dataset_service_date_range_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.backfill_dataset_service_date_range_zip.name
}
}
}
service_config {
available_memory = local.function_backfill_dataset_service_date_range_config.memory
available_cpu = local.function_backfill_dataset_service_date_range_config.available_cpu
timeout_seconds = local.function_backfill_dataset_service_date_range_config.timeout
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
# prevents multiline logs from being truncated on GCP console
ENV = var.environment
PYTHONNODEBUGRANGES = 0
}
dynamic "secret_environment_variables" {
for_each = local.function_backfill_dataset_service_date_range_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = lookup(secret_environment_variables.value, "secret", "${upper(var.environment)}_${secret_environment_variables.value["key"]}")
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
max_instance_request_concurrency = local.function_backfill_dataset_service_date_range_config.max_instance_request_concurrency
max_instance_count = local.function_backfill_dataset_service_date_range_config.max_instance_count
min_instance_count = local.function_backfill_dataset_service_date_range_config.min_instance_count
}
}
# 10. functions/export_csv cloud function
resource "google_cloudfunctions2_function" "export_csv" {
name = "${local.function_export_csv_config.name}"
project = var.project_id
description = local.function_export_csv_config.description
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
build_config {
runtime = var.python_runtime
entry_point = "${local.function_export_csv_config.entry_point}"
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.export_csv_zip.name
}
}
}
service_config {
environment_variables = {
DATASETS_BUCKET_NAME = data.google_storage_bucket.datasets_bucket.name
PROJECT_ID = var.project_id
ENVIRONMENT = var.environment
}
available_memory = local.function_export_csv_config.memory
timeout_seconds = local.function_export_csv_config.timeout
available_cpu = local.function_export_csv_config.available_cpu
max_instance_request_concurrency = local.function_export_csv_config.max_instance_request_concurrency
max_instance_count = local.function_export_csv_config.max_instance_count
min_instance_count = local.function_export_csv_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = "ALLOW_ALL"
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
dynamic "secret_environment_variables" {
for_each = local.function_export_csv_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 11. functions/update_feed_status cloud function
# Updates the Feed statuses based on latest dataset service date range
resource "google_cloudfunctions2_function" "update_feed_status" {
name = local.function_update_feed_status_config.name
description = local.function_update_feed_status_config.description
location = var.gcp_region
depends_on = [google_secret_manager_secret_iam_member.secret_iam_member]
project = var.project_id
build_config {
runtime = var.python_runtime
entry_point = local.function_update_feed_status_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.update_feed_status_zip.name
}
}
}
service_config {
available_memory = local.function_update_feed_status_config.memory
available_cpu = local.function_update_feed_status_config.available_cpu
timeout_seconds = local.function_update_feed_status_config.timeout
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
environment_variables = {
# prevents multiline logs from being truncated on GCP console
PYTHONNODEBUGRANGES = 0
}
dynamic "secret_environment_variables" {
for_each = local.function_update_feed_status_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = lookup(secret_environment_variables.value, "secret", "${upper(var.environment)}_${secret_environment_variables.value["key"]}")
version = "latest"
}
}
service_account_email = google_service_account.functions_service_account.email
max_instance_request_concurrency = local.function_update_feed_status_config.max_instance_request_concurrency
max_instance_count = local.function_update_feed_status_config.max_instance_count
min_instance_count = local.function_update_feed_status_config.min_instance_count
}
}
resource "google_cloud_scheduler_job" "export_csv_scheduler" {
name = "export-csv-scheduler-${var.environment}"
description = "Schedule the export_csv function"
time_zone = "Etc/UTC"
schedule = var.export_csv_schedule
region = var.gcp_region
paused = var.environment == "prod" ? false : true
depends_on = [google_cloudfunctions2_function.export_csv, google_cloudfunctions2_function_iam_member.export_csv_invoker]
http_target {
http_method = "POST"
uri = google_cloudfunctions2_function.export_csv.url
oidc_token {
service_account_email = google_service_account.functions_service_account.email
}
headers = {
"Content-Type" = "application/json"
}
}
# Export CSV can take several minutes to run (5?) so we need to give it a longer deadline
attempt_deadline = "600s"
}
resource "google_cloud_scheduler_job" "update_feed_status_scheduler" {
name = "update-feed-status-${var.environment}"
description = "Schedule the update_feed_status function daily"
time_zone = "Etc/UTC"
schedule = var.update_feed_status_schedule
region = var.gcp_region
paused = var.environment == "prod" ? false : true
depends_on = [google_cloudfunctions2_function.update_feed_status, google_cloudfunctions2_function_iam_member.update_feed_status_invoker]
http_target {
http_method = "POST"
uri = google_cloudfunctions2_function.update_feed_status.url
oidc_token {
service_account_email = google_service_account.functions_service_account.email
}
headers = {
"Content-Type" = "application/json"
}
}
attempt_deadline = "600s"
}
# IAM entry for all users to invoke the function
# 12. functions/reverse_geolocation_populate cloud function
resource "google_cloudfunctions2_function" "reverse_geolocation_populate" {
name = local.function_reverse_geolocation_populate_config.name
description = local.function_reverse_geolocation_populate_config.description
location = var.gcp_region
depends_on = [google_project_iam_member.event-receiving, google_secret_manager_secret_iam_member.secret_iam_member]
build_config {
runtime = var.python_runtime
entry_point = local.function_reverse_geolocation_populate_config.entry_point
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.reverse_geolocation_populate_zip.name
}
}
}
service_config {
environment_variables = {
PYTHONNODEBUGRANGES = 0
DB_REUSE_SESSION = "True"
}
available_memory = local.function_reverse_geolocation_populate_config.available_memory
timeout_seconds = local.function_reverse_geolocation_populate_config.timeout
available_cpu = local.function_reverse_geolocation_populate_config.available_cpu
max_instance_request_concurrency = local.function_reverse_geolocation_populate_config.max_instance_request_concurrency
max_instance_count = local.function_reverse_geolocation_populate_config.max_instance_count
min_instance_count = local.function_reverse_geolocation_populate_config.min_instance_count
service_account_email = google_service_account.functions_service_account.email
ingress_settings = local.function_reverse_geolocation_populate_config.ingress_settings
vpc_connector = data.google_vpc_access_connector.vpc_connector.id
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
dynamic "secret_environment_variables" {
for_each = local.function_reverse_geolocation_populate_config.secret_environment_variables
content {
key = secret_environment_variables.value["key"]
project_id = var.project_id
secret = "${upper(var.environment)}_${secret_environment_variables.value["key"]}"
version = "latest"
}
}
}
}
# 13.1 functions/reverse_geolocation - processor cloud function
resource "google_cloudfunctions2_function" "reverse_geolocation_processor" {
name = "${local.function_reverse_geolocation_config.name}-processor"
description = local.function_reverse_geolocation_config.description
location = var.gcp_region
depends_on = [
google_project_iam_member.event-receiving,
google_secret_manager_secret_iam_member.secret_iam_member,
]
build_config {
runtime = var.python_runtime
entry_point = "${local.function_reverse_geolocation_config.entry_point}_processor"
source {
storage_source {
bucket = google_storage_bucket.functions_bucket.name
object = google_storage_bucket_object.reverse_geolocation_zip.name
}
}
}
service_config {
environment_variables = {
PYTHONNODEBUGRANGES = 0
DATASETS_BUCKET_NAME_GTFS = "${var.datasets_bucket_name}-${var.environment}"
DATASETS_BUCKET_NAME_GBFS = "${var.gbfs_bucket_name}-${var.environment}"
}