-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
153 lines (131 loc) · 5.11 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
provider google {
credentials = file(var.credentials_file_name)
project = var.project_id
region = var.region_gcp
zone = var.zone_gcp
}
terraform {
backend "gcs" {
bucket = "[TERRAFORM-STATE-BUCKET]"
prefix = "state"
credentials = "./terraform.json"
}
}
provider "archive" {
}
resource "google_project_service" "cf-service" {
project = var.project_id
service = "bigqueryreservation.googleapis.com"
}
resource "google_project_service" "reservation-service" {
project = var.project_id
service = "cloudfunctions.googleapis.com"
}
resource "google_project_service" "iam-service" {
project = var.project_id
service = "iam.googleapis.com"
}
resource "null_resource" "delay_iam_0" {
provisioner "local-exec" {
command = "sleep 10"
}
depends_on = [google_project_service.iam-service]
}
// Create a service account for the two cloud functions
resource "google_service_account" "bq-flex-slots" {
account_id = "sa-bq-flex-slots"
display_name = "service account used by the two Cloud Functions related to BigQuery flex slots."
project = var.project_id
depends_on = [null_resource.delay_iam_0]
}
resource "null_resource" "delay_iam_1" {
provisioner "local-exec" {
command = "sleep 10"
}
depends_on = [google_service_account.bq-flex-slots]
}
// Assign roles to this service account
resource "google_project_iam_member" "cf-sa-access" {
for_each = var.cf_roles
project = var.project_id
member = "serviceAccount:${google_service_account.bq-flex-slots.email}"
role = each.value
depends_on = [null_resource.delay_iam_1]
}
// Allow terraform service account to deploy Cloud Functions with the newly created service account
resource "google_service_account_iam_member" "terraform-impersonation-cf-sa" {
service_account_id = google_service_account.bq-flex-slots.name
role = "roles/iam.serviceAccountUser"
member = "serviceAccount:${var.terraform_service_account_email}"
depends_on = [null_resource.delay_iam_1]
}
// Create a bucket to store the cloud function code
resource "google_storage_bucket" "cf_code" {
project = var.project_id
name = "${var.project_id}-cf-bq-flex-slots"
location = "EU"
bucket_policy_only = true
force_destroy = true
}
// Archive the start bq flex cloud function code and upload it as a zip on a bucket
data "archive_file" "function_start_flex_slots" {
type = "zip"
source_dir = "${path.module}/start_flex"
output_path = "${path.module}/start_flex_zip/CF.zip"
}
resource "google_storage_bucket_object" "zip_file_start_flex" {
name = "CF/start-flex-slots-${formatdate("YYYYMMDDhhmmss", timestamp())}"
bucket = google_storage_bucket.cf_code.name
source = data.archive_file.function_start_flex_slots.output_path
lifecycle {
ignore_changes = [name]
}
}
// Deploy the start bq flex slots Cloud Function
resource "google_cloudfunctions_function" "start_flex_slots" {
name = "start_bq_flex"
description = "[Managed by Terraform] This function gets triggered by a http GET call and will start BigQuery flex slots."
available_memory_mb = 128
source_archive_bucket = google_storage_bucket_object.zip_file_start_flex.bucket
source_archive_object = google_storage_bucket_object.zip_file_start_flex.name
entry_point = "main"
region = var.region_gcp
runtime = "python37"
service_account_email = google_service_account.bq-flex-slots.email
trigger_http = true
environment_variables = {
LOCATION = var.location_flex_slots
}
depends_on = [google_project_service.cf-service, google_service_account_iam_member.terraform-impersonation-cf-sa]
}
// Archive the stop bq flex cloud function code and upload it as a zip on a bucket
data "archive_file" "function_stop_flex_slots" {
type = "zip"
source_dir = "${path.module}/stop_flex"
output_path = "${path.module}/stop_flex_zip/CF.zip"
}
resource "google_storage_bucket_object" "zip_file_stop_flex" {
name = "CF/stop-flex-slots-${formatdate("YYYYMMDDhhmmss", timestamp())}"
bucket = google_storage_bucket.cf_code.name
source = data.archive_file.function_stop_flex_slots.output_path
lifecycle {
ignore_changes = [name]
}
}
// Deploy the stop bq flex slots Cloud Function
resource "google_cloudfunctions_function" "stop_flex_slots" {
name = "stop_bq_flex"
description = "[Managed by Terraform] This function gets triggered by a http GET call and will stop BigQuery flex slots."
available_memory_mb = 128
source_archive_bucket = google_storage_bucket_object.zip_file_stop_flex.bucket
source_archive_object = google_storage_bucket_object.zip_file_stop_flex.name
entry_point = "main"
region = var.region_gcp
runtime = "python37"
service_account_email = google_service_account.bq-flex-slots.email
trigger_http = true
environment_variables = {
LOCATION = var.location_flex_slots
}
depends_on = [google_project_service.cf-service, google_service_account_iam_member.terraform-impersonation-cf-sa]
}