-
Notifications
You must be signed in to change notification settings - Fork 16
/
workflows.tf
142 lines (125 loc) · 5.12 KB
/
workflows.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
/**
* 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.
*/
resource "google_project_service_identity" "workflows" {
provider = google-beta
project = module.project-services.project_id
service = "workflows.googleapis.com"
depends_on = [time_sleep.wait_after_apis_activate]
}
resource "google_service_account" "workflows_sa" {
project = module.project-services.project_id
account_id = "workflows-sa-${random_id.id.hex}"
display_name = "Workflows Service Account"
depends_on = [google_project_service_identity.workflows]
}
resource "google_project_iam_member" "workflows_sa_roles" {
for_each = toset([
"roles/workflows.admin",
"roles/storage.admin",
"roles/iam.serviceAccountTokenCreator",
"roles/iam.serviceAccountUser",
"roles/logging.logWriter",
"roles/dataproc.admin",
"roles/bigquery.admin",
"roles/dataplex.admin"
])
project = module.project-services.project_id
role = each.key
member = "serviceAccount:${google_service_account.workflows_sa.email}"
depends_on = [
google_service_account.workflows_sa
]
}
# Workflow to copy data from prod GCS bucket to private buckets
# NOTE: google_storage_bucket.<bucket>.name omits the `gs://` prefix.
# You can use google_storage_bucket.<bucket>.url to include the prefix.
resource "google_workflows_workflow" "copy_data" {
name = "copy-data"
project = module.project-services.project_id
region = var.region
description = "Copies data and performs project setup"
service_account = google_service_account.workflows_sa.email
source_contents = templatefile("${path.module}/src/yaml/copy-data.yaml", {
public_data_bucket = var.public_data_bucket,
textocr_images_bucket = google_storage_bucket.textocr_images_bucket.name,
ga4_images_bucket = google_storage_bucket.ga4_images_bucket.name,
tables_bucket = google_storage_bucket.tables_bucket.name,
dataplex_bucket = google_storage_bucket.dataplex_bucket.name,
images_zone_name = google_dataplex_zone.gcp_primary_raw.name,
tables_zone_name = google_dataplex_zone.gcp_primary_staging.name,
lake_name = google_dataplex_lake.gcp_primary.name
})
depends_on = [
google_project_iam_member.workflows_sa_roles,
google_project_iam_member.dataproc_sa_roles
]
}
# Workflow to set up project resources
resource "google_workflows_workflow" "project_setup" {
name = "project-setup"
project = module.project-services.project_id
region = var.region
description = "Copies data and performs project setup"
service_account = google_service_account.workflows_sa.email
source_contents = templatefile("${path.module}/src/yaml/project-setup.yaml", {})
depends_on = [
google_project_iam_member.workflows_sa_roles
]
}
# execute workflows after all resources are created
# # get a token to execute the workflows
data "google_client_config" "current" {
}
# # execute the copy data workflow
data "http" "call_workflows_copy_data" {
url = "https://workflowexecutions.googleapis.com/v1/projects/${module.project-services.project_id}/locations/${var.region}/workflows/${google_workflows_workflow.copy_data.name}/executions"
method = "POST"
request_headers = {
Accept = "application/json"
Authorization = "Bearer ${data.google_client_config.current.access_token}" }
depends_on = [
google_storage_bucket.textocr_images_bucket,
google_storage_bucket.ga4_images_bucket,
google_storage_bucket.tables_bucket
]
}
resource "time_sleep" "wait_after_copy_data" {
create_duration = "30s"
depends_on = [
data.http.call_workflows_copy_data
]
}
# execute the other project setup workflow
data "http" "call_workflows_project_setup" {
url = "https://workflowexecutions.googleapis.com/v1/projects/${module.project-services.project_id}/locations/${var.region}/workflows/${google_workflows_workflow.project_setup.name}/executions"
method = "POST"
request_headers = {
Accept = "application/json"
Authorization = "Bearer ${data.google_client_config.current.access_token}" }
depends_on = [
google_bigquery_dataset.gcp_lakehouse_ds,
time_sleep.wait_for_dataplex_discovery,
google_dataplex_asset.gcp_primary_ga4_obfuscated_sample_ecommerce,
google_dataplex_asset.gcp_primary_tables,
google_dataplex_asset.gcp_primary_textocr,
google_project_iam_member.connection_permission_grant,
google_project_iam_member.dataproc_sa_roles,
google_service_account.dataproc_service_account,
google_storage_bucket.provisioning_bucket,
google_storage_bucket.warehouse_bucket,
time_sleep.wait_after_copy_data
]
}