infctl-cli/gcloud/tf/main.tf

160 lines
3.8 KiB
HCL

// Compute
// ----------------------------------
// The instance for K3S
resource "google_compute_instance" "k3s" {
name = "k3s-vm-1"
machine_type = "e2-small" # This instance will have 2 Gb of RAM
zone = var.zone
tags = ["web"]
// Set the boot disk and the image (10 Gb)
boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
size = 10
}
}
// Configuration to be a Spot Instance, to reduce costs
scheduling {
automatic_restart = true
}
# scheduling {
# preemptible = false
# automatic_restart = true
# provisioning_model = "SPOT"
# instance_termination_action = "STOP"
# }
// attach a disk for K3S
attached_disk {
source = google_compute_disk.k3s_disk.id
device_name = "k3s-disk"
}
// attach a disk for app data
attached_disk {
source = google_compute_disk.app_data_disk.id
device_name = "app-data-disk"
}
network_interface {
network = "default"
// enable ephemeral ip
access_config {}
}
labels = {
env = var.env
region = var.region
app = var.app_name
sensitive = "false"
}
metadata_startup_script = file("scripts/k3s-vm-startup.sh")
allow_stopping_for_update = true
}
// Storage
// ----------------------------------
// The disk attached to the instance (15 Gb)
resource "google_compute_disk" "k3s_disk" {
name = "k3s-disk"
size = 15
type = "pd-standard"
zone = var.zone
}
// The disk for app data (20 Gb)
resource "google_compute_disk" "app_data_disk" {
name = "app-data-disk"
size = 20
type = "pd-standard"
zone = var.zone
}
// load balancer ....
# resource "google_compute_health_check" "http_health_check" {
# name = "http-health-check"
# check_interval_sec = 5
# timeout_sec = 5
# healthy_threshold = 2
# unhealthy_threshold = 2
# http_health_check {
# port = 80
# }
# }
resource "google_compute_http_health_check" "http_health_check" {
name = "http-health-check"
request_path = "/"
port = 80
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 2
}
# resource "google_compute_target_pool" "k3s_pool" {
# name = "k3s-target-pool"
# instances = [google_compute_instance.k3s.self_link]
# health_checks = [google_compute_health_check.http_health_check.self_link]
# }
resource "google_compute_target_pool" "k3s_pool" {
name = "k3s-target-pool"
instances = [google_compute_instance.k3s.self_link]
health_checks = [google_compute_http_health_check.http_health_check.self_link]
}
resource "google_compute_forwarding_rule" "http_forwarding_rule" {
name = "http-forwarding-rule"
target = google_compute_target_pool.k3s_pool.self_link
port_range = "80"
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL"
}
resource "google_compute_forwarding_rule" "https_forwarding_rule" {
name = "https-forwarding-rule"
target = google_compute_target_pool.k3s_pool.self_link
port_range = "443"
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL"
}
// ----------------------------------
data "google_project" "project" {
project_id = var.project_name # Use variable from tfvars
}
output "project_number" {
value = data.google_project.project.number
}
output "k3s_vm_public_ip" {
value = google_compute_instance.k3s.network_interface[0].access_config[0].nat_ip
description = "Ephemeral public IP of the k3s VM"
}
output "load_balancer_ip" {
value = google_compute_forwarding_rule.http_forwarding_rule.ip_address
description = "External IP address of the load balancer (HTTP)"
}