Skip to content

Commit

Permalink
update with load balancer module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Sun committed Oct 2, 2024
1 parent b9189d4 commit b7f09ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 86 deletions.
2 changes: 1 addition & 1 deletion ESLZ/vmss-windowsV2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module "vmss_windowsV2" {

location = var.location
subnets = local.subnets
resource_groups = local.resource_groups
resource_groups = local.resource_groups_all
userDefinedString = each.key
env = var.env
group = var.group
Expand Down
3 changes: 2 additions & 1 deletion ESLZ/vmss-windowsV2.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ vmss_windowsV2 = {
# azurerm_lb section
#
# edge_zone = "" # (Optional) Specifies the Edge Zone within the Azure Region where this Load Balancer should exist. Changing this forces a new Load Balancer to be created.

postfix = "01"
resource_group_name ="Project"
frontend_ip_configuration = {
feipc1 = {
subnet = "MAZ" # (Required) The name or the resource id of the Subnet which should be used for this IP Configuration
Expand Down
95 changes: 15 additions & 80 deletions loadbalancer.tf
Original file line number Diff line number Diff line change
@@ -1,81 +1,16 @@
resource "azurerm_lb" "loadbalancer" {
# Conditional creation of the load balancer if a load balancer configuration is provided for the VMSS
module "load_balancer" {
count = try(var.vmss.lb, null) != null ? 1 : 0

# Name and location settings for the load balancer
name = "${local.vmss_name}-lb"
location = var.location
resource_group_name = local.resource_group_name

edge_zone = try(var.vmss.lb.edge_zone, null)

# Frontend IP configuration - defines how the load balancer is exposed on the network
dynamic "frontend_ip_configuration" {
for_each = try(var.vmss.lb.frontend_ip_configuration, {})

content {
name = "${local.vmss_name}-${frontend_ip_configuration.key}-lbfe"
zones = try(frontend_ip_configuration.value.zones, null)
subnet_id = strcontains(frontend_ip_configuration.value.subnet, "/resourceGroups/") ? frontend_ip_configuration.value.subnet : var.subnets[frontend_ip_configuration.value.subnet].id
gateway_load_balancer_frontend_ip_configuration_id = try(frontend_ip_configuration.value.gateway_load_balancer_frontend_ip_configuration_id, null)
private_ip_address = try(frontend_ip_configuration.value.private_ip_address_allocation, "Static") == "Static" ? frontend_ip_configuration.value.private_ip_address : null
private_ip_address_allocation = try(frontend_ip_configuration.value.private_ip_address_allocation, "Static")
private_ip_address_version = try(frontend_ip_configuration.value.private_ip_address_version, "IPv4")
public_ip_address_id = try(frontend_ip_configuration.value.public_ip_address_id, null)
public_ip_prefix_id = try(frontend_ip_configuration.value.public_ip_prefix_id, null)
}
}

sku = try(var.vmss.lb.sku, "Standard")
sku_tier = try(var.vmss.lb.sku_tier, null)
tags = merge(var.tags, try(var.vmss.lb.tags, {}))
}

resource "azurerm_lb_probe" "loadbalancer-lbhp" {
for_each = try(var.vmss.lb.probes, {})

name = "${local.vmss_name}-${each.key}-lbhp"
loadbalancer_id = azurerm_lb.loadbalancer[0].id
protocol = try(each.value["protocol"], "Tcp")
port = each.value.port
probe_threshold = try(each.value["probe_threshold"], null)
request_path = try(each.value["request_path"], null)
interval_in_seconds = try(each.value["interval_in_seconds"], 5)
number_of_probes = try(each.value["number_of_probes"], 2)
}

resource "azurerm_lb_backend_address_pool" "loadbalancer-lbbp" {
count = try(var.vmss.lb, null) != null ? 1 : 0

name = "${local.vmss_name}-HA-lbbp"
loadbalancer_id = azurerm_lb.loadbalancer[0].id
synchronous_mode = try(var.vmss.lb.synchronous_mode, null)
dynamic "tunnel_interface" {
for_each = try(var.vmss.lb.tunnel_interfaces, {})
content {
identifier = tunnel_interface.value.identifier
type = tunnel_interface.value.type
protocol = tunnel_interface.value.protocol
port = tunnel_interface.value.port
}
}
virtual_network_id = try(var.vmss.lb.virtual_network_id, null)
}

resource "azurerm_lb_rule" "loadbalancer-lbr" {
for_each = try(var.vmss.lb.rules, {})

name = "${local.vmss_name}-${each.key}-lbr"
loadbalancer_id = azurerm_lb.loadbalancer[0].id
frontend_ip_configuration_name = "${local.vmss_name}-${each.value.frontend_ip_configuration_name}-lbfe"
protocol = each.value.protocol
frontend_port = each.value.frontend_port
backend_port = each.value.backend_port
backend_address_pool_ids = [azurerm_lb_backend_address_pool.loadbalancer-lbbp[0].id]
probe_id = try(each.value.probe_name, "") == "" ? null : azurerm_lb_probe.loadbalancer-lbhp["${each.value.probe_name}"].id
enable_floating_ip = try(each.value.enable_floating_ip, null)
idle_timeout_in_minutes = try(each.value.idle_timeout_in_minutes, 4)
load_distribution = try(each.value.load_distribution, null)
disable_outbound_snat = try(each.value.disable_outbound_snat, null)
enable_tcp_reset = try(each.value.enable_tcp_reset, null)
}
source = "github.com/canada-ca-terraform-modules/terraform-azurerm-caf-load_balancer.git"

location = var.location
subnets = var.subnets
resource_groups = var.resource_groups
userDefinedString = var.userDefinedString
tags = var.tags
env = var.env
group = var.group
project = var.project
load_balancer = var.vmss.lb
custom_data = try(var.vmss.lb.custom_data, false) != false ? base64encode(file("${path.cwd}/${var.vmss.lb.custom_data}")) : null
user_data = try(var.vmss.lb.user_data, false) != false ? base64encode(file("${path.cwd}/${var.vmss.lb.user_data}")) : null
}
2 changes: 1 addition & 1 deletion module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ resource "azurerm_windows_virtual_machine_scale_set" "vmss_windows" {
subnet_id = strcontains(ip_configuration.value.subnet, "/resourceGroups/") ? ip_configuration.value.subnet : var.subnets[ip_configuration.value.subnet].id
application_gateway_backend_address_pool_ids = try(ip_configuration.value.application_gateway_backend_address_pool_ids, [])
application_security_group_ids = try(ip_configuration.value.application_security_group_ids, [])
load_balancer_backend_address_pool_ids = try(var.vmss.lb, null) != null ? [azurerm_lb_backend_address_pool.loadbalancer-lbbp[0].id] : []
load_balancer_backend_address_pool_ids = try(var.vmss.lb, null) != null ? [module.load_balancer[0].loadbalancer_backend_address_pool.id] : []
load_balancer_inbound_nat_rules_ids = try(ip_configuration.value.load_balancer_inbound_nat_rules_ids, [])
dynamic "public_ip_address" {
for_each = try(ip_configuration.value.public_ip_address, null) != null ? [1] : []
Expand Down
6 changes: 3 additions & 3 deletions output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ output "vmss_windows" {
description = "VMSS Windows object"
}

output "loadbalancer" {
value = azurerm_lb.loadbalancer
description = "Load Balancer object"
output "loaddbalancer" {
description = "The availability_set object"
value = module.load_balancer
}

0 comments on commit b7f09ce

Please sign in to comment.