diff --git a/terraform/main.tf b/terraform/main.tf index 49d05c9..af9ec6e 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,6 +1,15 @@ module "private_server_group" { - source = "./modules/private-server-group" - location = var.location - network_zone = var.network_zone - server_count = var.server_count + source = "./modules/private-server-group" + location = var.location + network_zone = var.network_zone + server_count = var.server_count + server_subnetwork_ip_range = var.subnetwork_ip_range +} + +module "loadbalancer" { + source = "./modules/loadbalancer" + location = var.location + subnet_id = module.private_server_group.subnetwork_id + lb_internal_ip = var.lb_internal_ip + lb_external_ip = var.lb_external_ip } diff --git a/terraform/modules/loadbalancer/main.tf b/terraform/modules/loadbalancer/main.tf new file mode 100644 index 0000000..c863537 --- /dev/null +++ b/terraform/modules/loadbalancer/main.tf @@ -0,0 +1,26 @@ +resource "hcloud_load_balancer" "main" { + name = "main" + load_balancer_type = var.lb_type + location = var.location + algorithm { + type = var.lb_algorithm + } +} + +resource "hcloud_load_balancer_network" "main" { + load_balancer_id = hcloud_load_balancer.main.id + subnet_id = var.subnet_id + ip = var.lb_internal_ip +} + +resource "hcloud_load_balancer_target" "main" { + type = "label_selector" + load_balancer_id = hcloud_load_balancer.main.id + label_selector = "app=web" + use_private_ip = true +} + +resource "hcloud_load_balancer_service" "main" { + load_balancer_id = hcloud_load_balancer.main.id + protocol = "http" +} diff --git a/terraform/modules/loadbalancer/outputs.tf b/terraform/modules/loadbalancer/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/terraform/modules/loadbalancer/variables.tf b/terraform/modules/loadbalancer/variables.tf new file mode 100644 index 0000000..7610bae --- /dev/null +++ b/terraform/modules/loadbalancer/variables.tf @@ -0,0 +1,40 @@ +variable "location" { + description = <<-EOT + Location of the infrastructure. Needs to be aligned with network zone. + For more information visit https://docs.hetzner.com/cloud/general/locations/ + EOT + type = string +} + +variable "lb_type" { + description = "Type of loadbalancer" + type = string + default = "lb11" +} + +variable "lb_algorithm" { + description = "Type of the Load Balancer Algorithm. round_robin or least_connections" + type = string + default = "least_connections" +} + +variable "domain" { + description = "Domain name of the load balancer" + type = string + default = "wordpress-jr.senecops.com" +} + +variable "lb_external_ip" { + description = "IP address of the loadbalancer" + type = string +} + +variable "lb_internal_ip" { + description = "Internal IP address of the loadbalancer" + type = string +} + +variable "subnet_id" { + description = "ID of the subnetwork to attach the loadbalancer to" + type = string +} diff --git a/terraform/modules/loadbalancer/versions.tf b/terraform/modules/loadbalancer/versions.tf new file mode 100644 index 0000000..991eeea --- /dev/null +++ b/terraform/modules/loadbalancer/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + hcloud = { + source = "hetznercloud/hcloud" + version = "1.42.1" + } + } +} diff --git a/terraform/variables.tf b/terraform/variables.tf index 5cafb38..3d01a8c 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -18,3 +18,18 @@ variable "server_count" { description = "The number of servers to create" type = number } + +variable "subnetwork_ip_range" { + description = "Subnetwork IP range of the servers" + type = string +} + +variable "lb_external_ip" { + description = "IP address of the loadbalancer" + type = string +} + +variable "lb_internal_ip" { + description = "Internal IP address of the loadbalancer" + type = string +}