-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstance_group.tf
95 lines (77 loc) · 2.49 KB
/
instance_group.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
# Template for instances running Serverpod's Docker container.
resource "google_compute_instance_template" "serverpod" {
name = "serverpod-${var.runmode}-template"
description = "Instance template for Serverpod's Docker container."
machine_type = var.machine_type
disk {
source_image = "cos-cloud/cos-stable"
}
# Startup script that runs the Serverpod Docker container.
metadata_startup_script = <<-EOF
#!/bin/bash
useradd serverpod-user
usermod -aG docker serverpod-user
cd /home/serverpod-user
sudo -u serverpod-user docker-credential-gcr configure-docker --registries ${var.region}-docker.pkg.dev
sudo -u serverpod-user docker run -p 8080-8082:8080-8082 -e runmode=${var.runmode} -e serverid=$(hostname) ${var.region}-docker.pkg.dev/${var.project}/serverpod-${var.runmode}-container/serverpod:latest
EOF
network_interface {
network = google_compute_network.serverpod.name
access_config {
# Ephemeral public IP.
}
}
service_account {
email = var.service_account_email
scopes = ["cloud-platform"]
}
tags = ["serverpod-${var.runmode}-instance", "serverpod-${var.runmode}-instance-ssh"]
}
# Instance group manager that runs the Serverpod Docker container, with autoscaling and health checks.
resource "google_compute_instance_group_manager" "serverpod" {
name = "serverpod-${var.runmode}-group"
version {
instance_template = google_compute_instance_template.serverpod.id
}
base_instance_name = "serverpod-${var.runmode}"
zone = var.zone
named_port {
name = "api"
port = 8080
}
named_port {
name = "insights"
port = 8081
}
named_port {
name = "web"
port = 8082
}
auto_healing_policies {
health_check = google_compute_health_check.serverpod-instance-group.id
initial_delay_sec = 300
}
}
resource "google_compute_autoscaler" "serverpod" {
name = "serverpod-${var.runmode}-autoscaler"
zone = var.zone
target = google_compute_instance_group_manager.serverpod.id
autoscaling_policy {
min_replicas = var.autoscaling_min_size
max_replicas = var.autoscaling_max_size
cooldown_period = 60
cpu_utilization {
target = 0.6
}
}
}
resource "google_compute_health_check" "serverpod-instance-group" {
name = "serverpod-${var.runmode}-group-health-check"
timeout_sec = 5
check_interval_sec = 30
healthy_threshold = 2
unhealthy_threshold = 3
tcp_health_check {
port = "8080"
}
}