generated from snowplow-devops/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
189 lines (148 loc) · 5.15 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
locals {
module_name = "collector-pubsub-ce"
module_version = "0.5.1"
app_name = "stream-collector"
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
)
named_port_http = "http"
}
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 Stream Collector 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_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}"
}
# --- 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
}
# Needed to allow Health Checks and External Load Balancing services access to
# our server group.
#
# https://cloud.google.com/load-balancing/docs/health-check-concepts#ip-ranges
resource "google_compute_firewall" "ingress" {
project = (var.network_project_id != "") ? var.network_project_id : var.project_id
name = "${var.name}-traffic-in"
network = var.network
target_tags = [var.name]
allow {
protocol = "tcp"
ports = ["${var.ingress_port}"]
}
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
}
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"]
}
allow {
protocol = "udp"
ports = ["123"]
}
direction = "EGRESS"
destination_ranges = ["0.0.0.0/0"]
}
# --- CE: Instance group setup
locals {
collector_hocon = templatefile("${path.module}/templates/config.hocon.tmpl", {
port = var.ingress_port
paths = var.custom_paths
cookie_domain = var.cookie_domain
good_topic_name = var.good_topic_name
bad_topic_name = var.bad_topic_name
project_id = var.topic_project_id
byte_limit = var.byte_limit
record_limit = var.record_limit
time_limit_ms = var.time_limit_ms
telemetry_disable = !var.telemetry_enabled
telemetry_collector_uri = join("", module.telemetry.*.collector_uri)
telemetry_collector_port = 443
telemetry_secure = true
telemetry_user_provided_id = var.user_provided_id
telemetry_auto_gen_id = join("", module.telemetry.*.auto_generated_id)
telemetry_module_name = local.module_name
telemetry_module_version = local.module_version
})
startup_script = templatefile("${path.module}/templates/startup-script.sh.tmpl", {
accept_limited_use_license = var.accept_limited_use_license
port = var.ingress_port
config_b64 = base64encode(local.collector_hocon)
version = local.app_version
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
named_port_http = local.named_port_http
ingress_port = var.ingress_port
health_check_path = var.health_check_path
depends_on = [
google_compute_firewall.ingress
]
}