/
main.tf
286 lines (250 loc) · 8.63 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
/* -------------------------- */
/* --- TERRAFORM PROVIDER --- */
/* -------------------------- */
terraform {
required_providers {
# 2nd gen Cloud Functions were only supported in the beta provider at the time of creating this template
google = {
source = "hashicorp/google-beta"
version = "4.31.0"
}
}
/* OPTIONAL: Configure Terraform remote state
backend "gcs" {
bucket = "bucket-name-here"
prefix = "terraform/state"
}
*/
}
provider "google" {
project = var.project
region = var.region
zone = var.zone
}
/* -------------------------- */
/* ---- SERVICE ACCOUNT ----- */
/* -------------------------- */
# Service account
resource "google_service_account" "account" {
project = var.project
account_id = "sa-${var.app}-${terraform.workspace}"
display_name = "Service account for ${var.app}-${terraform.workspace}"
}
# Role grants
resource "google_project_iam_member" "run_invoker" {
role = "roles/run.invoker"
project = var.project
member = "serviceAccount:${google_service_account.account.email}"
}
resource "google_project_iam_member" "event_receiver" {
role = "roles/eventarc.eventReceiver"
project = var.project
member = "serviceAccount:${google_service_account.account.email}"
}
resource "google_project_iam_member" "artifactregistry_reader" {
role = "roles/artifactregistry.reader"
project = var.project
member = "serviceAccount:${google_service_account.account.email}"
}
/* -------------------------- */
/* --------- STORAGE -------- */
/* -------------------------- */
# Notifier configuration bucket
resource "google_storage_bucket" "notifierbucket" {
name = "${var.app}-${terraform.workspace}"
location = var.storage_location
storage_class = "STANDARD"
uniform_bucket_level_access = true
force_destroy = true
}
# Bucket IAM binding
resource "google_storage_bucket_iam_binding" "notifierbucket_binding" {
bucket = google_storage_bucket.notifierbucket.name
role = "roles/storage.objectViewer"
members = ["serviceAccount:${google_service_account.account.email}"]
}
# Data source configuration file
resource "google_storage_bucket_object" "configfile" {
name = "datasource-config/datasources.json"
bucket = google_storage_bucket.notifierbucket.name
source = "${path.root}/../configuration/datasources.json"
}
# Function zip archive
data "archive_file" "function_archive" {
type = "zip"
source_dir = "${path.root}/../functions/"
output_path = "${path.root}/.output/functions.zip"
}
# Function zip archive bucket object
resource "google_storage_bucket_object" "function_object" {
name = "functions/${data.archive_file.function_archive.output_md5}.zip"
bucket = google_storage_bucket.notifierbucket.name
source = data.archive_file.function_archive.output_path
}
/* -------------------------- */
/* --------- PUB/SUB -------- */
/* -------------------------- */
# Topic: add_to_manifest
resource "google_pubsub_topic" "add_to_manifest_topic" {
name = "pst-${var.app}-add-to-manifest-${terraform.workspace}"
}
# Topic: notify_manifest
resource "google_pubsub_topic" "notify_manifest_topic" {
name = "pst-${var.app}-notify-manifest-${terraform.workspace}"
}
# Topic IAM bindings
data "google_storage_project_service_account" "gcs_account" {
}
resource "google_pubsub_topic_iam_binding" "add_to_manifest_binding" {
topic = google_pubsub_topic.add_to_manifest_topic.id
role = "roles/pubsub.publisher"
members = ["serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"]
}
resource "google_pubsub_topic_iam_binding" "notify_manifest_binding" {
topic = google_pubsub_topic.notify_manifest_topic.id
role = "roles/pubsub.publisher"
members = ["serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"]
}
# Storage notification: add_to_manifest
resource "google_storage_notification" "notification" {
bucket = var.source_data_bucket
payload_format = "JSON_API_V1"
topic = google_pubsub_topic.add_to_manifest_topic.id
event_types = ["OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE"]
object_name_prefix = var.source_data_object_prefix
depends_on = [google_pubsub_topic_iam_binding.add_to_manifest_binding]
}
/* -------------------------- */
/* --------- NETWORK -------- */
/* -------------------------- */
# VPC
resource "google_compute_network" "cloud_function_network" {
name = "vpc-${var.app}-${terraform.workspace}"
auto_create_subnetworks = false
}
# VPC connector
resource "google_vpc_access_connector" "connector" {
name = "vpcac-${var.app}-${terraform.workspace}"
region = var.region
ip_cidr_range = var.cidr_range
network = google_compute_network.cloud_function_network.name
}
# IP address
resource "google_compute_address" "egress_ip_address" {
name = "ip-${var.app}-${terraform.workspace}"
region = var.region
}
# Router
resource "google_compute_router" "router" {
name = "gcr-${var.app}-${terraform.workspace}"
region = var.region
network = google_compute_network.cloud_function_network.name
}
# NAT
resource "google_compute_router_nat" "cloud_function_nat" {
name = "nat-${var.app}-${terraform.workspace}"
router = google_compute_router.router.name
region = google_compute_router.router.region
nat_ip_allocate_option = "MANUAL_ONLY"
nat_ips = google_compute_address.egress_ip_address.*.self_link
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
log_config {
enable = true
filter = "ERRORS_ONLY"
}
}
/* -------------------------- */
/* ------- FUNCTIONS -------- */
/* -------------------------- */
# Cloud function: add_to_manifest
resource "google_cloudfunctions2_function" "add_to_manifest_function" {
name = "gcf-${var.app}-add-to-manifest-${terraform.workspace}"
location = var.region
build_config {
runtime = "python310"
entry_point = "add_to_manifest"
source {
storage_source {
bucket = google_storage_bucket.notifierbucket.name
object = google_storage_bucket_object.function_object.name
}
}
}
service_config {
timeout_seconds = 60
available_memory = "256M"
environment_variables = {
NOTIFY_API_SECRET_ID = google_secret_manager_secret.notify_api.name
BUCKET_NAME = google_storage_bucket.notifierbucket.name
FILE_URL_PREFIX = var.file_url_prefix
}
vpc_connector = google_vpc_access_connector.connector.name
vpc_connector_egress_settings = "ALL_TRAFFIC"
max_instance_count = 1
service_account_email = google_service_account.account.email
}
event_trigger {
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.add_to_manifest_topic.id
retry_policy = "RETRY_POLICY_RETRY"
trigger_region = var.region
service_account_email = google_service_account.account.email
}
}
# Cloud function: notify_manifest
resource "google_cloudfunctions2_function" "notify_manifest_function" {
name = "gcf-${var.app}-notify-manifest-${terraform.workspace}"
location = var.region
build_config {
runtime = "python310"
entry_point = "notify_manifest"
source {
storage_source {
bucket = google_storage_bucket.notifierbucket.name
object = google_storage_bucket_object.function_object.name
}
}
}
service_config {
timeout_seconds = 60
available_memory = "256M"
environment_variables = {
NOTIFY_API_SECRET_ID = google_secret_manager_secret.notify_api.name
BUCKET_NAME = google_storage_bucket.notifierbucket.name
FILE_URL_PREFIX = var.file_url_prefix
}
vpc_connector = google_vpc_access_connector.connector.name
vpc_connector_egress_settings = "ALL_TRAFFIC"
max_instance_count = 1
service_account_email = google_service_account.account.email
}
event_trigger {
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.notify_manifest_topic.id
retry_policy = "RETRY_POLICY_RETRY"
trigger_region = var.region
service_account_email = google_service_account.account.email
}
}
/* -------------------------- */
/* ----- SECRET MANAGER ----- */
/* -------------------------- */
# Notify API secret
resource "google_secret_manager_secret" "notify_api" {
secret_id = "notify_api_${terraform.workspace}"
replication {
user_managed {
replicas {
location = var.replica_region
}
}
}
}
# Secret IAM binding
data "google_project" "project" {}
resource "google_secret_manager_secret_iam_binding" "binding" {
project = var.project
secret_id = google_secret_manager_secret.notify_api.secret_id
role = "roles/secretmanager.secretAccessor"
members = ["serviceAccount:${google_service_account.account.email}"]
}