-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
303 lines (270 loc) · 8.14 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
/**
* Copyright 2023 Google LLC
*
* 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.
*/
module "project_services" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 14.2"
disable_services_on_destroy = false
project_id = var.project_id
activate_apis = [
"serviceusage.googleapis.com",
"vision.googleapis.com",
"cloudfunctions.googleapis.com",
"serviceusage.googleapis.com",
"artifactregistry.googleapis.com",
"eventarc.googleapis.com",
"bigquery.googleapis.com",
"aiplatform.googleapis.com",
"storage.googleapis.com",
"cloudbuild.googleapis.com",
"run.googleapis.com",
"iam.googleapis.com",
"notebooks.googleapis.com",
"dataform.googleapis.com",
]
}
data "google_project" "project" {
project_id = var.project_id
depends_on = [
module.project_services,
]
}
resource "google_project_service_identity" "eventarc" {
provider = google-beta
project = data.google_project.project.project_id
service = "eventarc.googleapis.com"
depends_on = [
module.project_services,
]
}
resource "google_project_iam_member" "eventarc_sa_role" {
project = data.google_project.project.project_id
role = "roles/eventarc.serviceAgent"
member = "serviceAccount:${google_project_service_identity.eventarc.email}"
}
resource "null_resource" "previous_time" {}
# Gate till APIs are enabled
resource "time_sleep" "wait_for_apis" {
depends_on = [
null_resource.previous_time,
module.project_services,
google_project_iam_member.eventarc_sa_role,
]
create_duration = var.time_to_enable_apis
}
resource "random_id" "rand" {
byte_length = 4
}
data "archive_file" "webhook" {
type = "zip"
source_dir = var.webhook_path
output_path = abspath("./.tmp/${var.webhook_name}.zip")
}
resource "google_storage_bucket_object" "webhook" {
name = "${var.webhook_name}.${data.archive_file.webhook.output_base64sha256}.zip"
bucket = google_storage_bucket.main.name
source = data.archive_file.webhook.output_path
}
resource "google_service_account" "webhook" {
project = var.project_id
account_id = "webhook-service-account"
display_name = "Serverless Webhooks Service Account"
depends_on = [
module.project_services,
]
}
resource "google_project_iam_member" "webhook_sa_roles" {
project = var.project_id
for_each = toset([
"roles/run.invoker",
"roles/cloudfunctions.invoker",
"roles/storage.admin",
"roles/logging.logWriter",
"roles/artifactregistry.reader",
"roles/bigquery.dataEditor",
"roles/aiplatform.user",
])
role = each.key
member = "serviceAccount:${google_service_account.webhook.email}"
}
resource "google_cloudfunctions2_function" "webhook" {
project = var.project_id
name = var.webhook_name
location = var.region
build_config {
runtime = "python310"
entry_point = "entrypoint"
source {
storage_source {
bucket = "${var.bucket_name}-${random_id.rand.hex}"
object = google_storage_bucket_object.webhook.name
}
}
}
service_config {
service_account_email = google_service_account.webhook.email
max_instance_count = 5
available_memory = "4G"
available_cpu = 1
max_instance_request_concurrency = 5
timeout_seconds = var.gcf_timeout_seconds
environment_variables = {
PROJECT_ID = var.project_id
LOCATION = var.region
OUTPUT_BUCKET = google_storage_bucket.output.name
DATASET_ID = google_bigquery_dataset.default.dataset_id
TABLE_ID = google_bigquery_table.default.table_id
}
}
depends_on = [
module.project_services,
time_sleep.wait_for_apis,
google_project_iam_member.webhook_sa_roles,
]
}
resource "google_bigquery_dataset" "default" {
dataset_id = "summary_dataset"
project = var.project_id
depends_on = [
module.project_services,
]
}
resource "google_bigquery_table" "default" {
dataset_id = google_bigquery_dataset.default.dataset_id
table_id = "summary_table"
project = var.project_id
deletion_protection = false
schema = <<EOF
[
{
"name": "bucket",
"type": "STRING",
"description": "Source bucket of artifact"
},
{
"name": "filename",
"type": "STRING",
"description": "Filename of the source artifact"
},
{
"name": "extracted_text",
"type": "STRING",
"description": "Text extracted from the source artifact"
},
{
"name": "summary_uri",
"type": "STRING",
"description": "The Storage URI of the complete document text"
},
{
"name": "complete_text_uri",
"type": "STRING",
"description": "The Storage URI of the complete document text"
},
{
"name": "summary",
"type": "STRING",
"description": "Text summary generated by the LLM"
},
{
"name": "timestamp",
"type": "TIMESTAMP",
"description": "TeTimestamp that the processing occurred on"
}
]
EOF
}
resource "google_storage_bucket" "uploads" {
project = var.project_id
name = "${var.project_id}_uploads"
location = var.region
force_destroy = true
uniform_bucket_level_access = true
}
resource "google_storage_bucket" "output" {
project = var.project_id
name = "${var.project_id}_output"
location = var.region
force_destroy = true
uniform_bucket_level_access = true
}
resource "google_storage_bucket" "main" {
project = var.project_id
name = "${var.bucket_name}-${random_id.rand.hex}"
location = "US"
uniform_bucket_level_access = true
}
resource "google_service_account" "upload_trigger" {
project = var.project_id
account_id = "upload-trigger-service-account"
display_name = "Eventarc Service Account"
depends_on = [
module.project_services,
]
}
resource "google_project_iam_member" "event_receiver" {
project = var.project_id
role = "roles/eventarc.eventReceiver"
member = "serviceAccount:${google_service_account.upload_trigger.email}"
depends_on = [
module.project_services,
]
}
resource "google_project_iam_member" "run_invoker" {
project = var.project_id
role = "roles/run.invoker"
member = "serviceAccount:${google_service_account.upload_trigger.email}"
depends_on = [
module.project_services,
]
}
data "google_storage_project_service_account" "gcs_account" {
project = var.project_id
depends_on = [time_sleep.wait_for_apis]
}
resource "google_project_iam_member" "pubsub_publisher" {
project = var.project_id
role = "roles/pubsub.publisher"
member = "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
depends_on = [
module.project_services,
data.google_storage_project_service_account.gcs_account,
]
}
resource "google_eventarc_trigger" "summarization" {
project = var.project_id
name = "terraformdev"
location = var.region
matching_criteria {
attribute = "type"
value = "google.cloud.storage.object.v1.finalized"
}
matching_criteria {
attribute = "bucket"
value = google_storage_bucket.uploads.name
}
destination {
cloud_run_service {
service = google_cloudfunctions2_function.webhook.name
region = var.region
}
}
service_account = google_service_account.upload_trigger.email
depends_on = [
google_project_iam_member.event_receiver,
google_project_iam_member.run_invoker,
google_project_iam_member.pubsub_publisher,
]
}