forked from yupwei68/terraform-azurerm-loadbalancer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
74 lines (67 loc) · 3.5 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Azure load balancer module
resource "azurerm_resource_group" "azlb" {
name = "${var.resource_group_name}"
location = "${var.location}"
tags = "${var.tags}"
}
resource "azurerm_public_ip" "azlb" {
count = "${var.type == "public" ? 1 : 0}"
name = "${var.prefix}-publicIP"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.azlb.name}"
public_ip_address_allocation = "${var.public_ip_address_allocation}"
tags = "${var.tags}"
}
resource "azurerm_lb" "azlb" {
name = "${var.prefix}-lb"
resource_group_name = "${azurerm_resource_group.azlb.name}"
location = "${var.location}"
tags = "${var.tags}"
frontend_ip_configuration {
name = "${var.frontend_name}"
public_ip_address_id = "${var.type == "public" ? join("",azurerm_public_ip.azlb.*.id) : ""}"
subnet_id = "${var.frontend_subnet_id}"
private_ip_address = "${var.frontend_private_ip_address}"
private_ip_address_allocation = "${var.frontend_private_ip_address_allocation}"
}
}
resource "azurerm_lb_backend_address_pool" "azlb" {
resource_group_name = "${azurerm_resource_group.azlb.name}"
loadbalancer_id = "${azurerm_lb.azlb.id}"
name = "BackEndAddressPool"
}
resource "azurerm_lb_nat_rule" "azlb" {
count = "${length(var.remote_port)}"
resource_group_name = "${azurerm_resource_group.azlb.name}"
loadbalancer_id = "${azurerm_lb.azlb.id}"
name = "VM-${count.index}"
protocol = "tcp"
frontend_port = "5000${count.index + 1}"
backend_port = "${element(var.remote_port["${element(keys(var.remote_port), count.index)}"], 1)}"
frontend_ip_configuration_name = "${var.frontend_name}"
}
resource "azurerm_lb_probe" "azlb" {
count = "${length(var.lb_port)}"
resource_group_name = "${azurerm_resource_group.azlb.name}"
loadbalancer_id = "${azurerm_lb.azlb.id}"
name = "${element(keys(var.lb_port), count.index)}"
protocol = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 1)}"
port = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 2)}"
interval_in_seconds = "${var.lb_probe_interval}"
number_of_probes = "${var.lb_probe_unhealthy_threshold}"
}
resource "azurerm_lb_rule" "azlb" {
count = "${length(var.lb_port)}"
resource_group_name = "${azurerm_resource_group.azlb.name}"
loadbalancer_id = "${azurerm_lb.azlb.id}"
name = "${element(keys(var.lb_port), count.index)}"
protocol = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 1)}"
frontend_port = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 0)}"
backend_port = "${element(var.lb_port["${element(keys(var.lb_port), count.index)}"], 2)}"
frontend_ip_configuration_name = "${var.frontend_name}"
enable_floating_ip = false
backend_address_pool_id = "${azurerm_lb_backend_address_pool.azlb.id}"
idle_timeout_in_minutes = 5
probe_id = "${element(azurerm_lb_probe.azlb.*.id,count.index)}"
depends_on = ["azurerm_lb_probe.azlb"]
}