-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.tf
145 lines (127 loc) · 5.17 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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.
*/
# Upload the Cloud Function source code to a GCS bucket
## Define/create zip file for the Cloud Function source. This includes notebooks that will be uploaded
data "archive_file" "create_function_zip" {
type = "zip"
output_path = "${path.module}/tmp/function_source.zip"
source_dir = "${path.module}/src/templates/function/"
depends_on = [time_sleep.wait_after_apis]
}
resource "google_service_account" "cloud_function_manage_sa" {
project = module.project-services.project_id
account_id = "gemini-function-invoke-demo"
display_name = "Cloud Functions Service Account"
create_ignore_already_exists = var.create_ignore_service_accounts
depends_on = [time_sleep.wait_after_apis]
}
resource "google_project_iam_member" "function_manage_roles" {
for_each = toset([
"roles/aiplatform.user",
"roles/bigquery.admin", // Create jobs and modify BigQuery tables
"roles/cloudfunctions.admin", // Service account role to manage access to the remote function
"roles/iam.serviceAccountUser",
"roles/run.invoker", // Invoke Cloud Run to execute the function
"roles/storage.objectAdmin", // Read/write GCS files
"roles/dataform.admin", // Edit access code resources
"roles/iam.serviceAccountTokenCreator",
"roles/pubsub.publisher",
]
)
project = module.project-services.project_id
role = each.key
member = "serviceAccount:${google_service_account.cloud_function_manage_sa.email}"
depends_on = [google_project_iam_member.vertex_connection_manage_roles]
}
resource "google_cloudfunctions2_function" "customer_reviews" {
project = module.project-services.project_id
name = "analyze-reviews"
location = "us-central1"
description = "Use Gemini 1.0 Pro to analyze customer reviews and recommend actions"
build_config {
runtime = "python311"
entry_point = "run_it" # Set the entry point
source {
storage_source {
bucket = google_storage_bucket.function_source.name
object = google_storage_bucket_object.customer_reviews_function_source_upload.name
}
}
}
service_config {
max_instance_count = 10
#This value can be set to a non-zero integer to improve response time
min_instance_count = 0
available_memory = "2Gi"
timeout_seconds = 600
max_instance_request_concurrency = 1
available_cpu = "4"
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
"OUTPUT_BUCKET" : google_storage_bucket.data_source.name
}
}
depends_on = [google_project_iam_member.function_manage_roles]
}
# Create and deploy a Cloud Function to deploy notebooks
## Create the Cloud Function
resource "google_cloudfunctions2_function" "notebook_deploy_function" {
name = "deploy-notebooks"
project = module.project-services.project_id
location = var.region
description = "A Cloud Function that deploys sample notebooks."
build_config {
runtime = "python311"
entry_point = "run_it"
source {
storage_source {
bucket = google_storage_bucket.function_source.name
object = google_storage_bucket_object.notebook_function_source_upload.name
}
}
}
service_config {
max_instance_count = 1
# min_instance_count can be set to 1 to improve performance and responsiveness
min_instance_count = 0
available_memory = "512Mi"
timeout_seconds = 300
max_instance_request_concurrency = 1
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" : local.dataform_region
}
}
depends_on = [
time_sleep.wait_after_apis,
google_project_iam_member.function_manage_roles,
google_dataform_repository.notebook_repo,
google_dataform_repository_iam_member.workflow_manage_repo,
]
}
## Wait for Function deployment to complete
resource "time_sleep" "wait_after_function" {
create_duration = "5s"
depends_on = [google_cloudfunctions2_function.notebook_deploy_function, google_cloudfunctions2_function.customer_reviews]
}