-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmain.tf
321 lines (269 loc) · 8.67 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
locals {
exemplar_machine_type = "e2-medium"
node_machine_type = "e2-micro"
subnet_name = "${var.deployment_name}-subnet-01"
custom_network = var.network_id != ""
network_id = local.custom_network ? var.network_id : module.vpc[0].network_id
subnet_self_link = var.subnet_self_link != "" ? var.subnet_self_link : module.vpc[0].subnets["${var.region}/${local.subnet_name}"].self_link
network_project_id = var.network_project_id != "" ? var.network_project_id : var.project_id
lb_endpoint = "http://${module.gce-lb-http.external_ip}/"
}
# Enabling services in your GCP project
module "project-services" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "~> 17.0"
disable_services_on_destroy = false
project_id = var.project_id
enable_apis = var.enable_apis
activate_apis = [
"compute.googleapis.com"
]
}
module "vpc" {
count = local.custom_network ? 0 : 1
source = "terraform-google-modules/network/google"
version = "~> 9.0"
project_id = var.project_id
network_name = "${var.deployment_name}-network"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = local.subnet_name
subnet_ip = "10.10.10.0/24"
subnet_region = var.region
}
]
depends_on = [
module.project-services
]
}
resource "google_compute_firewall" "private-allow-ssh" {
name = "${var.deployment_name}-allow-ssh"
project = local.network_project_id
network = local.network_id
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["private-ssh"]
}
data "local_file" "index" {
filename = "${path.module}/files/index.html"
}
# Create Instance Exemplar on which to base Managed VMs
resource "google_compute_instance" "exemplar" {
name = "${var.deployment_name}-exemplar"
machine_type = local.exemplar_machine_type
zone = var.zone
project = var.project_id
labels = var.labels
tags = ["http-server", "private-ssh"]
metadata_startup_script = <<EOF
apt-get update -y
apt-get install nginx -y
printf '${data.local_file.index.content}' | tee /var/www/html/index.html
chgrp root /var/www/html/index.html
chown root /var/www/html/index.html
chmod +r /var/www/html/index.html
sleep 300
shutdown -h now
EOF
boot_disk {
auto_delete = true
device_name = "${var.deployment_name}-exemplar"
initialize_params {
image = "family/debian-12"
size = 200
type = "pd-standard"
}
}
network_interface {
subnetwork = local.subnet_self_link
subnetwork_project = local.network_project_id
access_config {
// Ephemeral public IP
}
}
}
resource "time_sleep" "startup_completion" {
create_duration = "120s"
depends_on = [google_compute_instance.exemplar]
}
resource "google_compute_snapshot" "main" {
project = var.project_id
name = "${var.deployment_name}-snapshot"
source_disk = google_compute_instance.exemplar.boot_disk[0].device_name
zone = var.zone
storage_locations = [var.region]
depends_on = [time_sleep.startup_completion]
labels = var.labels
}
# Create Disk Image for Instance Template
resource "google_compute_image" "exemplar" {
project = var.project_id
name = "${var.deployment_name}-latest"
family = var.deployment_name
source_snapshot = google_compute_snapshot.main.self_link
depends_on = [google_compute_snapshot.main]
labels = var.labels
}
# Create Instance Template
resource "google_compute_instance_template" "main" {
project = var.project_id
name = "${var.deployment_name}-template"
description = "This template is used to create app server instances."
tags = ["http-server", "private-ssh"]
labels = var.labels
metadata_startup_script = "sed -i.bak \"s/{{NODENAME}}/$HOSTNAME/\" /var/www/html/index.html"
instance_description = "${var.deployment_name} node"
machine_type = local.node_machine_type
can_ip_forward = false
// Create a new boot disk from an image
disk {
source_image = google_compute_image.exemplar.self_link
auto_delete = true
boot = true
}
network_interface {
subnetwork = local.subnet_self_link
subnetwork_project = local.network_project_id
}
}
resource "google_compute_target_pool" "main" {
project = var.project_id
name = "${var.deployment_name}-target-pool"
region = var.region
}
resource "google_compute_health_check" "autohealing" {
project = var.project_id
name = "${var.deployment_name}-autohealing-health-check"
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 10 # 50 seconds
http_health_check {
request_path = "/"
port = "80"
}
}
# Create Managed Instance Group
resource "google_compute_instance_group_manager" "main" {
project = var.project_id
provider = google-beta
name = "${var.deployment_name}-mig"
zone = var.zone
target_size = var.nodes
base_instance_name = "${var.deployment_name}-mig"
target_pools = [google_compute_target_pool.main.id]
version {
instance_template = google_compute_instance_template.main.id
}
all_instances_config {
labels = var.labels
}
named_port {
name = "http"
port = "80"
}
auto_healing_policies {
health_check = google_compute_health_check.autohealing.id
initial_delay_sec = 300
}
}
resource "google_compute_autoscaler" "main" {
project = var.project_id
name = "${var.deployment_name}-autoscaler"
zone = var.zone
target = google_compute_instance_group_manager.main.id
autoscaling_policy {
max_replicas = var.nodes * 2
min_replicas = var.nodes
cooldown_period = 60
cpu_utilization {
target = 0.5
}
}
}
module "gce-lb-http" {
source = "GoogleCloudPlatform/lb-http/google"
version = "~> 12.0"
project = var.project_id
name = "${var.deployment_name}-lb"
firewall_networks = [local.network_id]
backends = {
default = {
description = null
protocol = "HTTP"
port = "80"
port_name = "http"
timeout_sec = 10
enable_cdn = false
custom_request_headers = null
custom_response_headers = null
security_policy = null
connection_draining_timeout_sec = null
session_affinity = null
affinity_cookie_ttl_sec = null
health_check = {
check_interval_sec = null
timeout_sec = null
healthy_threshold = null
unhealthy_threshold = null
request_path = "/"
port = "80"
name = "${var.deployment_name}-health-chk"
host = null
logging = null
}
log_config = {
enable = true
sample_rate = 1.0
}
groups = [
{
group = google_compute_instance_group_manager.main.instance_group
balancing_mode = null
capacity_scaler = null
description = null
max_connections = null
max_connections_per_instance = null
max_connections_per_endpoint = null
max_rate = null
max_rate_per_instance = null
max_rate_per_endpoint = null
max_utilization = null
},
]
iap_config = {
enable = false
oauth2_client_id = null
oauth2_client_secret = null
}
}
}
}
data "http" "check" {
url = local.lb_endpoint
retry {
attempts = 50
max_delay_ms = 10 * 1000
min_delay_ms = 5 * 1000
}
depends_on = [module.gce-lb-http]
}