generated from snowplow-devops/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
183 lines (143 loc) · 4.9 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
locals {
module_name = "iglu-server-ce"
module_version = "0.5.1"
app_name = "iglu-server"
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 Iglu Server service account - ${var.name}"
}
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
}
# 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", var.db_port]
}
allow {
protocol = "udp"
ports = ["123"]
}
direction = "EGRESS"
destination_ranges = ["0.0.0.0/0"]
}
# --- CE: Instance group setup
locals {
# 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"
iglu_server_hocon = templatefile("${path.module}/templates/config.hocon.tmpl", {
port = var.ingress_port
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
patches_allowed = var.patches_allowed
super_api_key = lower(var.super_api_key)
})
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.iglu_server_hocon)
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
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
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
]
}