-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.tf
128 lines (114 loc) · 4.58 KB
/
function.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
/**
* 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.
*/
#Create a service account to manage the remote function and its permissions
resource "google_service_account" "cloud_function_manage_sa" {
project = module.project-services.project_id
account_id = "gemini-demo"
display_name = "Cloud Functions Service Account"
depends_on = [
time_sleep.wait_after_apis,
]
}
resource "google_project_iam_member" "function_manage_roles" {
for_each = toset([
"roles/cloudfunctions.admin", // Service account role to manage access to the remote function
"roles/run.invoker", // Service account role to invoke the remote function
"roles/storage.objectAdmin", // Read/write GCS files
"roles/bigquery.admin", // Create jobs and modify BigQuery tables
"roles/aiplatform.user", // Needs to predict from endpoints
"roles/aiplatform.serviceAgent", // Service account role
"roles/iam.serviceAccountUser"
]
)
project = module.project-services.project_id
role = each.key
member = "serviceAccount:${google_service_account.cloud_function_manage_sa.email}"
depends_on = [google_service_account.cloud_function_manage_sa]
}
## Create a Cloud Function to serve as the remote function for image analysis
resource "google_cloudfunctions2_function" "image_remote_function" {
name = "gemini-bq-demo-image"
project = module.project-services.project_id
location = var.region
description = "A Cloud Function that uses the Gemini Generative Model to analyze and describe images."
build_config {
runtime = "python311"
entry_point = "run_it"
source {
storage_source {
bucket = google_storage_bucket.function_source.name
object = google_storage_bucket_object.image_source_upload.name
}
}
}
service_config {
max_instance_count = 10
# min_instance_count can be set to 1 to improve performance and responsiveness
min_instance_count = 0
available_memory = "512mb"
timeout_seconds = 300
max_instance_request_concurrency = 5
available_cpu = "2"
ingress_settings = "ALLOW_ALL"
all_traffic_on_latest_revision = true
service_account_email = google_service_account.cloud_function_manage_sa.email
environment_variables = {
"PROJECT_ID" : module.project-services.project_id,
"REGION" : var.region }
}
depends_on = [time_sleep.wait_after_apis]
}
## Create a Cloud Function to serve as the remote function for image analysis
resource "google_cloudfunctions2_function" "text_remote_function" {
name = "gemini-bq-demo-text"
project = module.project-services.project_id
location = var.region
description = "A Cloud Function that uses the Gemini Generative Model to analyze and generate text."
build_config {
runtime = "python311"
entry_point = "run_it"
source {
storage_source {
bucket = google_storage_bucket.function_source.name
object = google_storage_bucket_object.text_source_upload.name
}
}
}
service_config {
max_instance_count = 10
# min_instance_count can be set to 1 to improve performance and responsiveness
min_instance_count = 0
available_memory = "512mb"
timeout_seconds = 300
max_instance_request_concurrency = 5
available_cpu = "2"
ingress_settings = "ALLOW_ALL"
all_traffic_on_latest_revision = true
service_account_email = google_service_account.cloud_function_manage_sa.email
environment_variables = {
"PROJECT_ID" : module.project-services.project_id,
"REGION" : var.region }
}
depends_on = [time_sleep.wait_after_apis]
}
# Wait until after the function is created to deconflict resource creation
resource "time_sleep" "wait_after_functions" {
create_duration = "10s"
depends_on = [
google_cloudfunctions2_function.image_remote_function,
google_cloudfunctions2_function.text_remote_function,
]
}