-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
113 lines (103 loc) · 3.55 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
# Generates an archive of the source code compressed as a .zip file.
data "archive_file" "source" {
type = "zip"
source_dir = "./src"
output_path = "${path.module}/function.zip"
}
# Add source code zip to the Cloud Function's bucket (Cloud_function_bucket)
resource "google_storage_bucket_object" "zip" {
source = data.archive_file.source.output_path
content_type = "application/zip"
name = "src-${data.archive_file.source.output_md5}.zip"
bucket = var.dcc_bucket
depends_on = [
data.archive_file.source
]
}
# Create new storage bucket for the HTAN center
resource "google_storage_bucket" "static" {
name = var.center_bucket
location = "us"
project = var.project_id
storage_class = "STANDARD"
uniform_bucket_level_access = true
cors {
origin = ["*"]
method = ["GET", "HEAD", "PUT", "POST"]
response_header = ["*"]
max_age_seconds = 3000
}
}
# Upload owner.txt files to the bucket
resource "google_storage_bucket_object" "default" {
name = "owner.txt"
source = "./owner.txt"
content_type = "text/plain"
bucket = google_storage_bucket.static.id
}
# Give Synapse service account permission to access bucket
resource "google_storage_bucket_iam_member" "member" {
bucket = google_storage_bucket.static.name
role = "roles/storage.legacyBucketReader"
member = var.synapse_sa
}
resource "google_storage_bucket_iam_member" "member_writer" {
bucket = google_storage_bucket.static.name
role = "roles/storage.legacyBucketWriter"
member = var.synapse_sa
}
resource "google_storage_bucket_iam_member" "member_viewer" {
bucket = google_storage_bucket.static.name
role = "roles/storage.objectViewer"
member = var.synapse_sa
}
# Create the Cloud function triggered by a `Finalize` event on the bucket
resource "google_cloudfunctions_function" "Cloud_function_add" {
name = "${var.center_bucket}-add"
description = "Cloud function triggered by file upload to gs://${var.center_bucket}"
runtime = "python311"
project = var.project_id
region = var.region
available_memory_mb = 512
source_archive_bucket = var.dcc_bucket
source_archive_object = google_storage_bucket_object.zip.name
timeout = 540
entry_point = "obj_add"
environment_variables = {
synapseProjectId = var.synapse_project_id
gcProjectName = var.project_id
}
event_trigger {
event_type = "google.storage.object.finalize"
resource = var.center_bucket
}
service_account_email = var.function_sa
depends_on = [
google_storage_bucket_object.zip
]
}
# Create the Cloud function triggered by a `Delete` event on the bucket
resource "google_cloudfunctions_function" "Cloud_function_delete" {
name = "${var.center_bucket}-delete"
description = "Cloud function triggered by file deletion from gs://${var.center_bucket}"
runtime = "python311"
project = var.project_id
region = var.region
available_memory_mb = 512
source_archive_bucket = var.dcc_bucket
source_archive_object = google_storage_bucket_object.zip.name
timeout = 540
entry_point = "obj_delete"
environment_variables = {
synapseProjectId = var.synapse_project_id
gcProjectName = var.project_id
}
event_trigger {
event_type = "google.storage.object.delete"
resource = var.center_bucket
}
service_account_email = var.function_sa
depends_on = [
google_storage_bucket_object.zip
]
}