-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
121 lines (99 loc) · 3.9 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
/**
* 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.
*/
locals {
default_data_ingestion_project_roles = [
"roles/pubsub.subscriber",
"roles/pubsub.editor",
"roles/storage.objectViewer",
"roles/dataflow.worker",
"roles/dataflow.developer"
]
bigquery_read_roles = [
"roles/bigquery.jobUser",
"roles/bigquery.dataEditor",
"roles/serviceusage.serviceUsageConsumer"
]
additional_project_roles = var.enable_bigquery_read_roles ? local.bigquery_read_roles : []
data_ingestion_project_roles = distinct(concat(local.additional_project_roles, local.default_data_ingestion_project_roles))
governance_project_roles = [
"roles/dlp.user",
"roles/dlp.inspectTemplatesReader",
"roles/dlp.deidentifyTemplatesReader"
]
cloudfunction_data_project_roles = [
"roles/eventarc.eventReceiver",
"roles/bigquery.dataEditor",
"roles/bigquery.jobUser"
]
}
//Dataflow controller service account
module "dataflow_controller_service_account" {
source = "terraform-google-modules/service-accounts/google"
version = "4.1.1"
project_id = var.data_ingestion_project_id
names = ["sa-dataflow-controller"]
display_name = "Cloud Dataflow controller service account"
}
resource "google_service_account_iam_member" "dataflow_controller_service_account_user" {
for_each = var.service_account_users
service_account_id = module.dataflow_controller_service_account.service_account.name
role = "roles/iam.serviceAccountUser"
member = each.value
}
resource "google_project_iam_member" "ingestion" {
for_each = toset(local.data_ingestion_project_roles)
project = var.data_ingestion_project_id
role = each.value
member = "serviceAccount:${module.dataflow_controller_service_account.email}"
}
resource "google_project_iam_member" "governance" {
for_each = toset(local.governance_project_roles)
project = var.data_governance_project_id
role = each.value
member = "serviceAccount:${module.dataflow_controller_service_account.email}"
}
//service account for storage
resource "google_service_account" "storage_writer_service_account" {
project = var.data_ingestion_project_id
account_id = "sa-storage-writer"
display_name = "Cloud Storage data writer service account"
}
//service account for Pub/sub
resource "google_service_account" "pubsub_writer_service_account" {
project = var.data_ingestion_project_id
account_id = "sa-pubsub-writer"
display_name = "Cloud PubSub data writer service account"
}
// Cloud Function service account
module "cloudfunction_controller_service_account" {
source = "terraform-google-modules/service-accounts/google"
version = "4.2"
project_id = var.data_ingestion_project_id
names = ["sa-cloudfunction-controller"]
display_name = "Cloud Function controller service account"
}
resource "google_project_iam_member" "cloudfunction" {
for_each = toset(local.cloudfunction_data_project_roles)
project = var.data_ingestion_project_id
role = each.value
member = "serviceAccount:${module.cloudfunction_controller_service_account.email}"
}
resource "google_service_account_iam_member" "cloud_function_sa_service_account_user" {
for_each = var.service_account_users
service_account_id = module.cloudfunction_controller_service_account.service_account.name
role = "roles/iam.serviceAccountUser"
member = each.value
}