-
Notifications
You must be signed in to change notification settings - Fork 912
/
main.tf
313 lines (291 loc) · 10.2 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
304
305
306
307
308
309
310
311
312
313
/**
* Copyright 2024 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.
*/
locals {
iam_principals = merge(
var.groups.gcp-ml-viewer == null ? {} : {
"group:${var.groups.gcp-ml-viewer}" = [
"roles/aiplatform.viewer",
"roles/artifactregistry.reader",
"roles/dataflow.viewer",
"roles/logging.viewer",
"roles/storage.objectViewer"
]
},
var.groups.gcp-ml-ds == null ? {} : {
"group:${var.groups.gcp-ml-ds}" = [
"roles/aiplatform.admin",
"roles/artifactregistry.admin",
"roles/bigquery.dataEditor",
"roles/bigquery.jobUser",
"roles/bigquery.user",
"roles/cloudbuild.builds.editor",
"roles/cloudfunctions.developer",
"roles/dataflow.developer",
"roles/dataflow.worker",
"roles/iam.serviceAccountUser",
"roles/logging.logWriter",
"roles/logging.viewer",
"roles/notebooks.admin",
"roles/pubsub.editor",
"roles/serviceusage.serviceUsageConsumer",
"roles/storage.admin"
]
},
var.groups.gcp-ml-eng == null ? {} : {
"group:${var.groups.gcp-ml-eng}" = [
"roles/aiplatform.admin",
"roles/artifactregistry.admin",
"roles/bigquery.dataEditor",
"roles/bigquery.jobUser",
"roles/bigquery.user",
"roles/dataflow.developer",
"roles/dataflow.worker",
"roles/iam.serviceAccountUser",
"roles/logging.logWriter",
"roles/logging.viewer",
"roles/serviceusage.serviceUsageConsumer",
"roles/storage.admin"
]
}
)
shared_vpc_project = try(var.network_config.host_project, null)
subnet = (
local.use_shared_vpc
? var.network_config.subnet_self_link
: values(module.vpc-local[0].subnet_self_links)[0]
)
vpc = (
local.use_shared_vpc
? var.network_config.network_self_link
: module.vpc-local[0].self_link
)
use_shared_vpc = var.network_config != null
shared_vpc_bindings = {
"roles/compute.networkUser" = [
"robot-df", "notebooks"
]
}
shared_vpc_role_members = {
robot-df = module.project.service_agents.dataflow.iam_email
notebooks = module.project.service_agents.notebooks.iam_email
}
# reassemble in a format suitable for for_each
shared_vpc_bindings_map = {
for binding in flatten([
for role, members in local.shared_vpc_bindings : [
for member in members : { role = role, member = member }
]
]) : "${binding.role}-${binding.member}" => binding
}
}
module "gcs-bucket" {
count = var.bucket_name == null ? 0 : 1
source = "../../../modules/gcs"
project_id = module.project.project_id
name = var.bucket_name
prefix = var.prefix
location = var.region
storage_class = "REGIONAL"
versioning = false
encryption_key = var.service_encryption_keys.storage
force_destroy = !var.deletion_protection
}
# Default bucket for Cloud Build to prevent error: "'us' violates constraint ‘gcp.resourceLocations’"
# https://stackoverflow.com/questions/53206667/cloud-build-fails-with-resource-location-constraint
module "gcs-bucket-cloudbuild" {
source = "../../../modules/gcs"
project_id = module.project.project_id
name = "${module.project.project_id}_cloudbuild"
location = var.region
storage_class = "REGIONAL"
versioning = false
encryption_key = var.service_encryption_keys.storage
force_destroy = !var.deletion_protection
}
module "bq-dataset" {
count = var.dataset_name == null ? 0 : 1
source = "../../../modules/bigquery-dataset"
project_id = module.project.project_id
id = var.dataset_name
location = var.region
encryption_key = var.service_encryption_keys.bq
}
module "vpc-local" {
count = local.use_shared_vpc ? 0 : 1
source = "../../../modules/net-vpc"
project_id = module.project.project_id
name = "vertex"
subnets = [
{
name = "subnet-${var.region}",
region = var.region,
ip_cidr_range = "10.5.0.0/24"
enable_private_access = true
}
]
psa_configs = [{
ranges = {
"vertex" : "10.13.0.0/18"
}
routes = null
}]
}
module "firewall" {
count = local.use_shared_vpc ? 0 : 1
source = "../../../modules/net-vpc-firewall"
project_id = module.project.project_id
network = module.vpc-local[0].name
default_rules_config = {
disabled = true
}
ingress_rules = {
dataflow-ingress = {
description = "Dataflow service."
direction = "INGRESS"
action = "allow"
sources = ["dataflow"]
targets = ["dataflow"]
ranges = []
use_service_accounts = false
rules = [{ protocol = "tcp", ports = ["12345-12346"] }]
extra_attributes = {}
}
}
}
module "cloudnat" {
count = local.use_shared_vpc ? 0 : 1
source = "../../../modules/net-cloudnat"
project_id = module.project.project_id
region = var.region
name = "default"
router_network = module.vpc-local[0].self_link
}
module "project" {
source = "../../../modules/project"
name = var.project_config.project_id
parent = var.project_config.parent
billing_account = var.project_config.billing_account_id
project_create = var.project_config.billing_account_id != null
prefix = var.prefix
iam_by_principals = local.iam_principals
iam_bindings_additive = {
# we manage aiplatform.user additively since it is also granted to
# the vertex-shtune service agent by the project module
aiplatform-user-mlops = {
member = module.service-account-mlops.iam_email
role = "roles/aiplatform.user"
}
aiplatform-user-notebook = {
member = module.service-account-notebook.iam_email
role = "roles/aiplatform.user"
}
}
iam = {
"roles/artifactregistry.reader" = [module.service-account-mlops.iam_email]
"roles/artifactregistry.writer" = [module.service-account-github.iam_email]
"roles/bigquery.dataEditor" = [
module.service-account-mlops.iam_email,
module.service-account-notebook.iam_email
]
"roles/bigquery.jobUser" = [
module.service-account-mlops.iam_email,
module.service-account-notebook.iam_email
]
"roles/bigquery.user" = [module.service-account-mlops.iam_email, module.service-account-notebook.iam_email]
"roles/cloudbuild.builds.editor" = [
module.service-account-mlops.iam_email,
module.service-account-github.iam_email
]
"roles/cloudfunctions.invoker" = [module.service-account-mlops.iam_email]
"roles/dataflow.developer" = [module.service-account-mlops.iam_email]
"roles/dataflow.worker" = [module.service-account-mlops.iam_email]
"roles/iam.serviceAccountUser" = [
module.service-account-mlops.iam_email,
module.service-account-notebook.iam_email,
module.service-account-github.iam_email,
module.project.service_agents.cloudbuild.iam_email
]
"roles/logging.logWriter" = [
module.service-account-notebook.iam_email,
]
"roles/monitoring.metricWriter" = [
module.service-account-mlops.iam_email,
module.service-account-notebook.iam_email,
]
"roles/run.invoker" = [module.service-account-mlops.iam_email]
"roles/serviceusage.serviceUsageConsumer" = [
module.service-account-mlops.iam_email,
module.service-account-github.iam_email,
module.service-account-notebook.iam_email,
]
"roles/storage.admin" = [
module.service-account-mlops.iam_email,
module.service-account-github.iam_email,
module.service-account-notebook.iam_email
]
}
labels = var.labels
service_encryption_key_ids = {
"aiplatform.googleapis.com" = compact([var.service_encryption_keys.aiplatform])
"bigquery.googleapis.com" = compact([var.service_encryption_keys.bq])
"compute.googleapis.com" = compact([var.service_encryption_keys.notebooks])
#"cloudbuild.googleapis.com" = compact([var.service_encryption_keys.storage])
"notebooks.googleapis.com" = compact([var.service_encryption_keys.notebooks])
"secretmanager.googleapis.com" = compact([var.service_encryption_keys.secretmanager])
"storage.googleapis.com" = compact([var.service_encryption_keys.storage])
}
services = [
"aiplatform.googleapis.com",
"artifactregistry.googleapis.com",
"bigquery.googleapis.com",
"bigquerystorage.googleapis.com",
"cloudbuild.googleapis.com",
"containerfilesystem.googleapis.com",
"compute.googleapis.com",
"datacatalog.googleapis.com",
"dataflow.googleapis.com",
"iam.googleapis.com",
"ml.googleapis.com",
"monitoring.googleapis.com",
"notebooks.googleapis.com",
"secretmanager.googleapis.com",
"servicenetworking.googleapis.com",
"serviceusage.googleapis.com",
"stackdriver.googleapis.com",
"storage.googleapis.com",
"storage-component.googleapis.com"
]
shared_vpc_service_config = local.shared_vpc_project == null ? null : {
attach = true
host_project = local.shared_vpc_project
}
}
module "service-account-mlops" {
source = "../../../modules/iam-service-account"
name = "${var.prefix}-sa-mlops"
project_id = module.project.project_id
}
resource "google_project_iam_member" "shared_vpc" {
count = local.use_shared_vpc ? 1 : 0
project = var.network_config.host_project
role = "roles/compute.networkUser"
member = module.project.service_agents.notebooks.iam_email
}
resource "google_sourcerepo_repository" "code-repo" {
count = var.repo_name == null ? 0 : 1
name = var.repo_name
project = module.project.project_id
}