-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
136 lines (111 loc) · 3.79 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
terraform {
required_providers {
zipper = {
version = "~>0.1"
source = "ArthurHlt/zipper"
}
}
}
provider "google" {
project = var.projectName
region = var.region
}
provider "zipper" {
skip_ssl_validation = true
}
#################################################################################
## Service Account and IAM ##
resource "google_service_account" "sa" {
account_id = var.saName
display_name = "Service Account with access to add billing information"
}
resource "google_organization_iam_member" "organization" {
org_id = var.orgID
role = "roles/billing.user"
member = google_service_account.sa.email
}
resource "google_project_iam_member" "log-writer" {
project = var.projectName
role = "roles/storage.objectCreator"
member = "serviceAccount:${google_logging_organization_sink.projectCreate.writer_identity}"
}
resource "google_project_iam_member" "cloudfunctions" {
for_each = toset(var.saRoles)
project = var.projectName
role = each.value
member = "serviceAccount:${google_service_account.sa.email}"
}
#################################################################################
## PubSub ##
resource "google_pubsub_topic" "topic" {
name = var.pubsubTopic
message_retention_duration = "86600s"
}
#################################################################################
## Storage and Data ##
resource "google_storage_bucket" "codeBucket" {
name = "${var.projectName}-lp-gcf"
location = "US"
uniform_bucket_level_access = true
}
resource "google_storage_bucket_object" "object" {
name = var.codeZip
bucket = google_storage_bucket.codeBucket.name
source = zipper_file.cloudFunctionCode.output_path
}
resource "google_storage_bucket" "log-bucket" {
name = var.loggingBucketName
location = "US"
}
resource "zipper_file" "cloudFunctionCode" {
source = "${path.module}/code"
output_path = "${path.module}/files/${var.codeZip}"
}
#################################################################################
## Log Sink ##
resource "google_logging_organization_sink" "projectCreate" {
name = "projectCreated"
description = "Sink for only projects being created"
org_id = var.orgID
# Can export to pubsub, cloud storage, or bigquery
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
# Log all WARN or higher severity messages relating to instances
filter = "protoPayload.methodName=\"CreateProject\""
}
#################################################################################
## Cloud Function ##
resource "google_cloudfunctions2_function" "create_project_watch_function" {
name = var.funcName
location = var.region
description = "Function for applying billing when a new project is created"
build_config {
runtime = "python311"
entry_point = "main" # Set the entry point
source {
storage_source {
bucket = google_storage_bucket.codeBucket.name
object = google_storage_bucket_object.object.name
}
}
}
service_config {
max_instance_count = 1
min_instance_count = 0
available_memory = "256Mi"
timeout_seconds = 60
max_instance_request_concurrency = 80
available_cpu = "1"
environment_variables = {
LP_BILLING_ACCOUNT = var.billingAccount
}
ingress_settings = "ALLOW_INTERNAL_ONLY"
all_traffic_on_latest_revision = true
service_account_email = google_service_account.sa.email
}
event_trigger {
trigger_region = var.region
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.topic.id
retry_policy = "RETRY_POLICY_RETRY"
}
}