|
| 1 | +data "google_compute_image" "debian" { |
| 2 | + family = "debian-12" |
| 3 | + project = "debian-cloud" |
| 4 | +} |
| 5 | + |
| 6 | +resource "google_compute_instance" "service" { |
| 7 | + name = var.service_name |
| 8 | + machine_type = "e2-micro" |
| 9 | + zone = "${var.region}-b" |
| 10 | + project = var.project_id |
| 11 | + |
| 12 | + boot_disk { |
| 13 | + initialize_params { |
| 14 | + image = data.google_compute_image.debian.self_link |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + network_interface { |
| 19 | + network = var.network |
| 20 | + subnetwork = var.subnet |
| 21 | + } |
| 22 | + |
| 23 | + tags = [var.service_name, "allow-ssh"] |
| 24 | + |
| 25 | + metadata_startup_script = <<-EOF |
| 26 | + #!/bin/bash |
| 27 | + cat > /opt/health_server.py << 'PY' |
| 28 | + from http.server import BaseHTTPRequestHandler, HTTPServer |
| 29 | +
|
| 30 | + class Handler(BaseHTTPRequestHandler): |
| 31 | + def do_GET(self): |
| 32 | + if self.path in ("/health", "/healthz", "/"): |
| 33 | + self.send_response(200) |
| 34 | + self.send_header("Content-Type", "text/plain") |
| 35 | + self.end_headers() |
| 36 | + self.wfile.write(b"ok\n") |
| 37 | + return |
| 38 | + self.send_response(404) |
| 39 | + self.end_headers() |
| 40 | +
|
| 41 | + def log_message(self, format, *args): |
| 42 | + return |
| 43 | +
|
| 44 | + if __name__ == "__main__": |
| 45 | + HTTPServer(("0.0.0.0", 9090), Handler).serve_forever() |
| 46 | + PY |
| 47 | + nohup python3 /opt/health_server.py & |
| 48 | + EOF |
| 49 | + |
| 50 | + labels = { |
| 51 | + service = var.service_name |
| 52 | + environment = "production" |
| 53 | + team = var.team |
| 54 | + managed-by = "terraform" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +# Service ingress — allows application and health-check traffic from internal ranges. |
| 59 | +# target_tags binds this rule to instances carrying the service_name tag. |
| 60 | +resource "google_compute_firewall" "service_ingress" { |
| 61 | + name = "${var.service_name}-ingress" |
| 62 | + network = var.network |
| 63 | + project = var.project_id |
| 64 | + description = "Allow ${var.service_name} traffic on service and health-check ports" |
| 65 | + |
| 66 | + allow { |
| 67 | + protocol = "tcp" |
| 68 | + ports = [tostring(var.service_port), "9090"] |
| 69 | + } |
| 70 | + |
| 71 | + source_ranges = var.allowed_source_ranges |
| 72 | + target_tags = [var.service_name] |
| 73 | +} |
| 74 | + |
| 75 | +# Dedicated rule for Google Cloud health-check probes (source ranges are fixed |
| 76 | +# Google-owned prefixes). Also targets by service_name tag. |
| 77 | +resource "google_compute_firewall" "health_check" { |
| 78 | + name = "${var.service_name}-health-check" |
| 79 | + network = var.network |
| 80 | + project = var.project_id |
| 81 | + description = "Allow Google Cloud health-check probes to ${var.service_name}" |
| 82 | + |
| 83 | + allow { |
| 84 | + protocol = "tcp" |
| 85 | + ports = ["9090"] |
| 86 | + } |
| 87 | + |
| 88 | + source_ranges = ["35.191.0.0/16", "130.211.0.0/22"] |
| 89 | + target_tags = [var.service_name] |
| 90 | +} |
| 91 | + |
| 92 | +resource "google_compute_health_check" "service" { |
| 93 | + name = "${var.service_name}-health" |
| 94 | + project = var.project_id |
| 95 | + |
| 96 | + tcp_health_check { |
| 97 | + port = 9090 |
| 98 | + } |
| 99 | + |
| 100 | + check_interval_sec = 10 |
| 101 | + timeout_sec = 5 |
| 102 | + healthy_threshold = 2 |
| 103 | + unhealthy_threshold = 3 |
| 104 | +} |
| 105 | + |
| 106 | +resource "google_monitoring_notification_channel" "oncall" { |
| 107 | + display_name = "${var.service_name} On-Call (${var.team})" |
| 108 | + type = "pubsub" |
| 109 | + project = var.project_id |
| 110 | + |
| 111 | + labels = { |
| 112 | + topic = var.alert_topic |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +resource "google_monitoring_alert_policy" "health" { |
| 117 | + display_name = "${var.service_name} Health Check Failed" |
| 118 | + combiner = "OR" |
| 119 | + project = var.project_id |
| 120 | + |
| 121 | + conditions { |
| 122 | + display_name = "${var.service_name} Instance Not Running" |
| 123 | + |
| 124 | + condition_threshold { |
| 125 | + filter = "resource.type = \"gce_instance\" AND resource.labels.instance_id = \"${google_compute_instance.service.instance_id}\" AND metric.type = \"compute.googleapis.com/instance/uptime\"" |
| 126 | + comparison = "COMPARISON_LT" |
| 127 | + threshold_value = 1 |
| 128 | + duration = "300s" |
| 129 | + |
| 130 | + aggregations { |
| 131 | + alignment_period = "300s" |
| 132 | + per_series_aligner = "ALIGN_RATE" |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + notification_channels = [google_monitoring_notification_channel.oncall.id] |
| 138 | + |
| 139 | + user_labels = { |
| 140 | + service = var.service_name |
| 141 | + team = var.team |
| 142 | + } |
| 143 | +} |
0 commit comments