-
Notifications
You must be signed in to change notification settings - Fork 2
/
webservers-compute.tf
176 lines (149 loc) · 5.85 KB
/
webservers-compute.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
// Definition of active web servers for the web application
resource "random_string" "random_web_server_suffix" {
length = 8
lower = true
upper = false
special = false
keepers = {
webapp_bucket_name = local.bucket_name
deployment_bundle_filename = local.webapp_deployment_bundle_filename
deployment_bundle_url = local.webapp_deployment_bundle_url
nginx_docker_image_version = var.webserver_docker_image_version
startup_script = md5(file("${path.module}/scripts/webserver_vm_startup_script.sh"))
vm_flag_preemptible = var.vm_flag_preemptible
}
}
// Access to Available compute zones in the given region --- //
data "google_compute_zones" "available" {
count = length(var.webserver_deployment_regions)
region = var.webserver_deployment_regions[count.index]
}
// Service Account --- //
resource "google_service_account" "gcp_service_acc_apis" {
project = var.project_id
account_id = "${var.module_wide_prefix_scope}-svcacc-${random_string.random_web_server_suffix.result}"
display_name = "${var.module_wide_prefix_scope}-GCP-service-account"
}
// Roles ---
resource "google_project_iam_member" "logging-writer" {
project = var.project_id
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.gcp_service_acc_apis.email}"
}
resource "google_project_iam_member" "monitoring-writer" {
project = var.project_id
role = "roles/monitoring.metricWriter"
member = "serviceAccount:${google_service_account.gcp_service_acc_apis.email}"
}
// --- /Service Account Configuration/ ---
// Instance Template --- //
resource "google_compute_instance_template" "webserver_template" {
count = length(var.webserver_deployment_regions)
name = "${var.module_wide_prefix_scope}-${count.index}-webserver-template-${random_string.random_web_server_suffix.result}"
description = "Open Targets Platform Web Server node template"
instance_description = "Open Targets Platform Web Server node, docker image version ${var.webserver_docker_image_version}"
region = var.webserver_deployment_regions[count.index]
tags = local.webapp_webserver_template_tags
machine_type = local.webapp_webserver_template_machine_type
can_ip_forward = false
scheduling {
automatic_restart = !var.vm_flag_preemptible
on_host_maintenance = var.vm_flag_preemptible ? "TERMINATE" : "MIGRATE"
preemptible = var.vm_flag_preemptible
provisioning_model = var.vm_flag_preemptible ? "SPOT" : "STANDARD"
instance_termination_action = var.vm_flag_preemptible ? "STOP" : null
}
disk {
source_image = local.webapp_webserver_template_source_image
auto_delete = true
disk_type = "pd-ssd"
boot = true
mode = "READ_WRITE"
}
network_interface {
network = var.network_name
subnetwork = var.network_subnet_name
}
lifecycle {
create_before_destroy = true
}
metadata = {
startup-script = templatefile(
"${path.module}/scripts/webserver_vm_startup_script.sh",
{
dev_www_data_disk = local.webapp_webserver_disk_web_dev_name
deployment_bundle_url = local.webapp_deployment_bundle_url
deployment_bundle_filename = local.webapp_deployment_bundle_filename
docker_image_version = var.webserver_docker_image_version
}
)
google-logging-enabled = true
}
service_account {
email = google_service_account.gcp_service_acc_apis.email
scopes = ["cloud-platform", "logging-write", "monitoring-write"]
}
}
// TODO - Helath Check --- //
resource "google_compute_health_check" "webserver_healthcheck" {
name = "${var.module_wide_prefix_scope}-webserver-healthcheck"
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 10
tcp_health_check {
port = local.webapp_webserver_port
}
}
// TODO - RegMIG --- //
resource "google_compute_region_instance_group_manager" "regmig_webserver" {
count = length(var.webserver_deployment_regions)
provider = google-beta
name = "${var.module_wide_prefix_scope}-${count.index}-regmig-webserver"
region = var.webserver_deployment_regions[count.index]
base_instance_name = "${var.module_wide_prefix_scope}-${count.index}-webserver"
depends_on = [
google_compute_instance_template.webserver_template,
google_compute_firewall.vpc_netfw_webserver_node,
null_resource.webapp_provisioner
]
// Instance Template
version {
instance_template = google_compute_instance_template.webserver_template[count.index].id
}
target_size = var.deployment_target_size
named_port {
name = local.webapp_webserver_port_name
port = local.webapp_webserver_port
}
auto_healing_policies {
health_check = google_compute_health_check.webserver_healthcheck.id
initial_delay_sec = 20
}
update_policy {
type = "PROACTIVE"
instance_redistribution_type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = length(data.google_compute_zones.available[count.index].names)
max_unavailable_fixed = 0
min_ready_sec = 30
}
}
// TODO - Autoscalers --- //
resource "google_compute_region_autoscaler" "autoscaler_webserver" {
count = length(var.webserver_deployment_regions)
name = "${var.module_wide_prefix_scope}-${count.index}-autoscaler"
region = var.webserver_deployment_regions[count.index]
target = google_compute_region_instance_group_manager.regmig_webserver[count.index].id
autoscaling_policy {
max_replicas = length(data.google_compute_zones.available[count.index].names) * 2
min_replicas = 1
cooldown_period = 30
load_balancing_utilization {
target = 0.5
}
cpu_utilization {
target = 0.75
}
}
}