-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
68 lines (58 loc) · 2.1 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
resource "random_string" "random" {
length = 5
special = false
numeric = false
}
resource "google_pubsub_topic" "topic" {
project = "${var.project_id}"
name = lower("finops-shutdown-${random_string.random.result}")
}
resource "google_cloud_scheduler_job" "job" {
region = "${var.region}"
project = "${var.project_id}"
name = lower("finops-shutdown-${random_string.random.result}")
description = lower("finops-shutdown-${random_string.random.result}")
schedule = "*/30 * * * *"
depends_on = [ google_pubsub_topic.topic ]
pubsub_target {
topic_name = google_pubsub_topic.topic.id
data = base64encode("{\"name\":\"function:shutdown\", \"action\":\"reduce\"}")
}
}
resource "google_storage_bucket" "bucket" {
project = var.project_id
name = lower("finops-shutdown-${random_string.random.result}")
location = "${var.region}"
}
data "archive_file" "shutdown" {
type = "zip"
source_dir = "functions/shutdown"
output_path = "functions/shutdown/shutdown.zip"
}
resource "google_storage_bucket_object" "shutdown" {
name = "${random_string.random.result}.zip"
bucket = google_storage_bucket.bucket.name
source = data.archive_file.shutdown.output_path
}
resource "google_cloudfunctions_function" "shutdown" {
name = lower("finops-shutdown-${random_string.random.result}")
description = lower("finops-shutdown-${random_string.random.result}")
runtime = "go120"
region = var.region
project = "${var.project_id}"
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.shutdown.name
entry_point = "ProcessPubSub"
event_trigger {
event_type = "google.pubsub.topic.publish"
resource = google_pubsub_topic.topic.name
}
}
resource "google_cloudfunctions_function_iam_member" "invoker" {
project = google_cloudfunctions_function.shutdown.project
region = google_cloudfunctions_function.shutdown.region
cloud_function = google_cloudfunctions_function.shutdown.name
role = "roles/cloudfunctions.invoker"
member = "allUsers"
}