generated from snowplow-devops/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
229 lines (183 loc) · 6.22 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
locals {
module_name = "postgres-loader-pubsub-ce"
module_version = "0.4.1"
app_name = "snowplow-postgres-loader"
app_version = var.app_version
local_labels = {
name = var.name
app_name = local.app_name
app_version = replace(local.app_version, ".", "-")
module_name = local.module_name
module_version = replace(local.module_version, ".", "-")
}
labels = merge(
var.labels,
local.local_labels
)
}
module "telemetry" {
source = "snowplow-devops/telemetry/snowplow"
version = "0.5.0"
count = var.telemetry_enabled ? 1 : 0
user_provided_id = var.user_provided_id
cloud = "GCP"
region = var.region
app_name = local.app_name
app_version = local.app_version
module_name = local.module_name
module_version = local.module_version
}
# --- IAM: Service Account setup
resource "google_service_account" "sa" {
account_id = var.name
display_name = "Snowplow PG Loader service account - ${var.name}"
}
resource "google_project_iam_member" "sa_pubsub_viewer" {
project = var.project_id
role = "roles/pubsub.viewer"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_pubsub_subscriber" {
project = var.project_id
role = "roles/pubsub.subscriber"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_pubsub_publisher" {
project = var.project_id
role = "roles/pubsub.publisher"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_logging_log_writer" {
project = var.project_id
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.sa.email}"
}
resource "google_project_iam_member" "sa_cloud_sql_client" {
project = var.project_id
role = "roles/cloudsql.client"
member = "serviceAccount:${google_service_account.sa.email}"
}
# --- CE: Firewall rules
resource "google_compute_firewall" "ingress_ssh" {
project = (var.network_project_id != "") ? var.network_project_id : var.project_id
name = "${var.name}-ssh-in"
network = var.network
target_tags = [var.name]
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = var.ssh_ip_allowlist
}
resource "google_compute_firewall" "egress" {
project = (var.network_project_id != "") ? var.network_project_id : var.project_id
name = "${var.name}-traffic-out"
network = var.network
target_tags = [var.name]
allow {
protocol = "tcp"
ports = ["80", "443", var.db_port]
}
allow {
protocol = "udp"
ports = ["123"]
}
direction = "EGRESS"
destination_ranges = ["0.0.0.0/0"]
}
# --- CE: Instance group setup
resource "google_pubsub_subscription" "in" {
name = var.name
topic = var.in_topic_name
expiration_policy {
ttl = ""
}
labels = local.labels
}
locals {
resolvers_raw = concat(var.default_iglu_resolvers, var.custom_iglu_resolvers)
resolvers_open = [
for resolver in local.resolvers_raw : merge(
{
name = resolver["name"],
priority = resolver["priority"],
vendorPrefixes = resolver["vendor_prefixes"],
connection = {
http = {
uri = resolver["uri"]
}
}
}
) if resolver["api_key"] == ""
]
resolvers_closed = [
for resolver in local.resolvers_raw : merge(
{
name = resolver["name"],
priority = resolver["priority"],
vendorPrefixes = resolver["vendor_prefixes"],
connection = {
http = {
uri = resolver["uri"]
apikey = resolver["api_key"]
}
}
}
) if resolver["api_key"] != ""
]
resolvers = flatten([
local.resolvers_open,
local.resolvers_closed
])
iglu_resolver = templatefile("${path.module}/templates/iglu_resolver.json.tmpl", { resolvers = jsonencode(local.resolvers) })
# Note: If we are provided a valid DB Instance Name leverage CloudSQL proxy
db_host = var.db_instance_name == "" ? var.db_host : "127.0.0.1"
config = templatefile("${path.module}/templates/config.json.tmpl", {
project_id = var.project_id
in_subscription_name = google_pubsub_subscription.in.name
in_max_concurrent_checkpoints = var.in_max_concurrent_checkpoints
db_host = local.db_host
db_port = var.db_port
db_name = var.db_name
db_username = var.db_username
db_password = var.db_password
db_max_connections = var.db_max_connections
schema_name = var.schema_name
purpose = var.purpose
})
startup_script = templatefile("${path.module}/templates/startup-script.sh.tmpl", {
accept_limited_use_license = var.accept_limited_use_license
config_b64 = base64encode(local.config)
iglu_resolver_b64 = base64encode(local.iglu_resolver)
version = local.app_version
db_host = local.db_host
db_port = var.db_port
db_name = var.db_name
db_username = var.db_username
db_password = var.db_password
schema_name = var.schema_name
db_instance_name = var.db_instance_name
cloud_sql_proxy_enabled = var.db_instance_name != ""
telemetry_script = join("", module.telemetry.*.gcp_ubuntu_20_04_user_data)
gcp_logs_enabled = var.gcp_logs_enabled
java_opts = var.java_opts
})
}
module "service" {
source = "snowplow-devops/service-ce/google"
version = "0.1.0"
user_supplied_script = local.startup_script
name = var.name
instance_group_version_name = "${local.app_name}-${local.app_version}"
labels = local.labels
region = var.region
network = var.network
subnetwork = var.subnetwork
ubuntu_20_04_source_image = var.ubuntu_20_04_source_image
machine_type = var.machine_type
target_size = var.target_size
ssh_block_project_keys = var.ssh_block_project_keys
ssh_key_pairs = var.ssh_key_pairs
service_account_email = google_service_account.sa.email
associate_public_ip_address = var.associate_public_ip_address
}