Add terraform module to deploy a hcloud loadbalancer

This commit is contained in:
Johannes Rothe 2023-08-17 22:05:53 +02:00
parent a00abcd884
commit bb0aa44a1a
Signed by: onjen
GPG Key ID: 73F092605AF3286C
6 changed files with 102 additions and 4 deletions

View File

@ -1,6 +1,15 @@
module "private_server_group" { module "private_server_group" {
source = "./modules/private-server-group" source = "./modules/private-server-group"
location = var.location location = var.location
network_zone = var.network_zone network_zone = var.network_zone
server_count = var.server_count 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
} }

View File

@ -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"
}

View File

@ -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
}

View File

@ -0,0 +1,8 @@
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
version = "1.42.1"
}
}
}

View File

@ -18,3 +18,18 @@ variable "server_count" {
description = "The number of servers to create" description = "The number of servers to create"
type = number 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
}