Skip to content

Commit da29f31

Browse files
authored
Correct README examples and add enhancement (#26)
1 parent b40b8fc commit da29f31

5 files changed

Lines changed: 112 additions & 54 deletions

File tree

README.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,80 @@ characteristics:
1515
Public loadbalancer example:
1616

1717
```hcl
18-
variable "resource_group_name" {
19-
default = "my-terraform-lb"
18+
provider "azurerm" {
19+
features {}
2020
}
2121
22-
variable "location" {
23-
default = "eastus"
22+
resource "azurerm_resource_group" "example" {
23+
name = "example-lb"
24+
location = "West Europe"
2425
}
2526
2627
module "mylb" {
2728
source = "Azure/loadbalancer/azurerm"
28-
resource_group_name = "${var.resource_group_name}"
29-
location = "${var.location}"
29+
resource_group_name = azurerm_resource_group.example.name
3030
prefix = "terraform-test"
3131
32-
"remote_port" {
32+
remote_port = {
3333
ssh = ["Tcp", "22"]
3434
}
3535
36-
"lb_port" {
36+
lb_port = {
3737
http = ["80", "Tcp", "80"]
3838
}
39-
}
4039
41-
module "network" {
42-
source = "Azure/network/azurerm"
43-
location = "${var.location}"
44-
resource_group_name = "${var.resource_group_name}"
40+
lb_probe = {
41+
http = ["Tcp", "80", ""]
42+
}
43+
4544
}
45+
4646
```
4747

4848
Private loadbalancer example:
4949

5050
```hcl
51+
provider "azurerm" {
52+
features {}
53+
}
54+
55+
resource "azurerm_resource_group" "example" {
56+
name = "example-lb"
57+
location = "West Europe"
58+
}
59+
5160
module "mylb" {
5261
source = "Azure/loadbalancer/azurerm"
53-
location = "westus"
62+
resource_group_name = azurerm_resource_group.example.name
5463
type = "private"
55-
frontend_subnet_id = "${module.network.vnet_subnets[0]}"
64+
frontend_subnet_id = module.network.vnet_subnets[0]
5665
frontend_private_ip_address_allocation = "Static"
5766
frontend_private_ip_address = "10.0.1.6"
67+
lb_sku = "Standard"
5868
59-
"remote_port" {
69+
remote_port = {
6070
ssh = ["Tcp", "22"]
6171
}
6272
63-
"lb_port" {
73+
lb_port = {
6474
http = ["80", "Tcp", "80"]
6575
https = ["443", "Tcp", "443"]
6676
}
6777
68-
"tags" {
78+
lb_probe = {
79+
http = ["Tcp", "80", ""]
80+
http2 = ["Http", "1443", "/"]
81+
}
82+
83+
tags = {
6984
cost-center = "12345"
7085
source = "terraform"
7186
}
7287
}
7388
7489
module "network" {
7590
source = "Azure/network/azurerm"
76-
resource_group_name = "myapp"
77-
location = "westus"
91+
resource_group_name = azurerm_resource_group.example.name
7892
address_space = "10.0.0.0/16"
7993
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
8094
subnet_names = ["subnet1", "subnet2", "subnet3"]

main.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# Azure load balancer module
2-
resource "azurerm_resource_group" "azlb" {
3-
name = var.resource_group_name
4-
location = var.location
5-
tags = var.tags
2+
data "azurerm_resource_group" "azlb" {
3+
name = var.resource_group_name
64
}
75

86
resource "azurerm_public_ip" "azlb" {
97
count = var.type == "public" ? 1 : 0
108
name = "${var.prefix}-publicIP"
11-
resource_group_name = azurerm_resource_group.azlb.name
12-
location = azurerm_resource_group.azlb.location
9+
resource_group_name = data.azurerm_resource_group.azlb.name
10+
location = coalesce(var.location, data.azurerm_resource_group.azlb.location)
1311
allocation_method = var.allocation_method
1412
tags = var.tags
1513
}
1614

1715
resource "azurerm_lb" "azlb" {
1816
name = "${var.prefix}-lb"
19-
resource_group_name = azurerm_resource_group.azlb.name
20-
location = azurerm_resource_group.azlb.location
17+
resource_group_name = data.azurerm_resource_group.azlb.name
18+
location = coalesce(var.location, data.azurerm_resource_group.azlb.location)
19+
sku = var.lb_sku
2120
tags = var.tags
2221

2322
frontend_ip_configuration {
@@ -31,14 +30,14 @@ resource "azurerm_lb" "azlb" {
3130

3231
resource "azurerm_lb_backend_address_pool" "azlb" {
3332
name = "BackEndAddressPool"
34-
resource_group_name = azurerm_resource_group.azlb.name
33+
resource_group_name = data.azurerm_resource_group.azlb.name
3534
loadbalancer_id = azurerm_lb.azlb.id
3635
}
3736

3837
resource "azurerm_lb_nat_rule" "azlb" {
3938
count = length(var.remote_port)
4039
name = "VM-${count.index}"
41-
resource_group_name = azurerm_resource_group.azlb.name
40+
resource_group_name = data.azurerm_resource_group.azlb.name
4241
loadbalancer_id = azurerm_lb.azlb.id
4342
protocol = "tcp"
4443
frontend_port = "5000${count.index + 1}"
@@ -47,20 +46,21 @@ resource "azurerm_lb_nat_rule" "azlb" {
4746
}
4847

4948
resource "azurerm_lb_probe" "azlb" {
50-
count = length(var.lb_port)
51-
name = element(keys(var.lb_port), count.index)
52-
resource_group_name = azurerm_resource_group.azlb.name
49+
count = length(var.lb_probe)
50+
name = element(keys(var.lb_probe), count.index)
51+
resource_group_name = data.azurerm_resource_group.azlb.name
5352
loadbalancer_id = azurerm_lb.azlb.id
54-
protocol = element(var.lb_port[element(keys(var.lb_port), count.index)], 1)
55-
port = element(var.lb_port[element(keys(var.lb_port), count.index)], 2)
53+
protocol = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 0)
54+
port = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 1)
5655
interval_in_seconds = var.lb_probe_interval
5756
number_of_probes = var.lb_probe_unhealthy_threshold
57+
request_path = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 2)
5858
}
5959

6060
resource "azurerm_lb_rule" "azlb" {
6161
count = length(var.lb_port)
6262
name = element(keys(var.lb_port), count.index)
63-
resource_group_name = azurerm_resource_group.azlb.name
63+
resource_group_name = data.azurerm_resource_group.azlb.name
6464
loadbalancer_id = azurerm_lb.azlb.id
6565
protocol = element(var.lb_port[element(keys(var.lb_port), count.index)], 1)
6666
frontend_port = element(var.lb_port[element(keys(var.lb_port), count.index)], 0)

outputs.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
output "azurerm_resource_group_tags" {
22
description = "the tags provided for the resource group"
3-
value = "${azurerm_resource_group.azlb.tags}"
3+
value = data.azurerm_resource_group.azlb.tags
44
}
55

66
output "azurerm_resource_group_name" {
77
description = "name of the resource group provisioned"
8-
value = "${azurerm_resource_group.azlb.name}"
8+
value = data.azurerm_resource_group.azlb.name
99
}
1010

1111
output "azurerm_lb_id" {
1212
description = "the id for the azurerm_lb resource"
13-
value = "${azurerm_lb.azlb.id}"
13+
value = azurerm_lb.azlb.id
1414
}
1515

1616
output "azurerm_lb_frontend_ip_configuration" {
1717
description = "the frontend_ip_configuration for the azurerm_lb resource"
18-
value = "${azurerm_lb.azlb.frontend_ip_configuration}"
18+
value = azurerm_lb.azlb.frontend_ip_configuration
1919
}
2020

2121
output "azurerm_lb_probe_ids" {
2222
description = "the ids for the azurerm_lb_probe resources"
23-
value = "${azurerm_lb_probe.azlb.*.id}"
23+
value = azurerm_lb_probe.azlb.*.id
2424
}
2525

2626
output "azurerm_lb_nat_rule_ids" {
2727
description = "the ids for the azurerm_lb_nat_rule resources"
28-
value = "${azurerm_lb_nat_rule.azlb.*.id}"
28+
value = azurerm_lb_nat_rule.azlb.*.id
2929
}
3030

3131
output "azurerm_public_ip_id" {
3232
description = "the id for the azurerm_lb_public_ip resource"
33-
value = "${azurerm_public_ip.azlb.*.id}"
33+
value = azurerm_public_ip.azlb.*.id
3434
}
3535

3636
output "azurerm_public_ip_address" {
3737
description = "the ip address for the azurerm_lb_public_ip resource"
38-
value = "${azurerm_public_ip.azlb.*.ip_address}"
38+
value = azurerm_public_ip.azlb.*.ip_address
3939
}
4040

4141
output "azurerm_lb_backend_address_pool_id" {
4242
description = "the id for the azurerm_lb_backend_address_pool resource"
43-
value = "${azurerm_lb_backend_address_pool.azlb.id}"
43+
value = azurerm_lb_backend_address_pool.azlb.id
4444
}

test/fixture/main.tf

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,49 @@ resource "random_id" "rg_name" {
66
byte_length = 8
77
}
88

9+
resource "azurerm_resource_group" "test" {
10+
name = "example-lb-${random_id.rg_name.hex}"
11+
location = "West Europe"
12+
}
13+
914
module "mylb" {
10-
source = "../../"
11-
resource_group_name = "${random_id.rg_name.hex}"
12-
location = "${var.location}"
13-
prefix = "${random_id.rg_name.hex}"
15+
source = "../.."
16+
resource_group_name = azurerm_resource_group.test.name
17+
type = "private"
18+
frontend_subnet_id = module.network.vnet_subnets[0]
19+
frontend_private_ip_address_allocation = "Static"
20+
frontend_private_ip_address = "10.0.1.6"
21+
lb_sku = "Standard"
1422

1523
remote_port = {
1624
ssh = ["Tcp", "22"]
1725
}
1826

1927
lb_port = {
20-
http = ["80", "Tcp", "80"]
28+
http = ["80", "Tcp", "80"]
29+
https = ["443", "Tcp", "443"]
30+
}
31+
32+
lb_probe = {
33+
http = ["Tcp", "80", ""]
34+
http2 = ["Http", "1443", "/"]
35+
}
36+
37+
tags = {
38+
cost-center = "12345"
39+
source = "terraform"
40+
}
41+
}
42+
43+
module "network" {
44+
source = "Azure/network/azurerm"
45+
resource_group_name = azurerm_resource_group.test.name
46+
address_space = "10.0.0.0/16"
47+
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
48+
subnet_names = ["subnet1", "subnet2", "subnet3"]
49+
50+
tags = {
51+
environment = "dev"
52+
costcenter = "it"
2153
}
2254
}

variables.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
variable "location" {
2-
description = "(Required) The location/region where the core network will be created. The full list of Azure regions can be found at https://azure.microsoft.com/regions"
2+
description = "(Optional) The location/region where the core network will be created. The full list of Azure regions can be found at https://azure.microsoft.com/regions"
3+
default = ""
34
}
45

56
variable "resource_group_name" {
6-
description = "(Required) The name of the resource group where the load balancer resources will be placed."
7-
default = "azure_lb-rg"
7+
description = "(Required) The name of the resource group where the load balancer resources will be imported."
88
}
99

1010
variable "prefix" {
@@ -18,7 +18,8 @@ variable "remote_port" {
1818
}
1919

2020
variable "lb_port" {
21-
description = "Protocols to be used for lb health probes and rules. [frontend_port, protocol, backend_port]"
21+
description = "Protocols to be used for lb rules. Format as [frontend_port, protocol, backend_port]"
22+
type = map(any)
2223
default = {}
2324
}
2425

@@ -70,3 +71,14 @@ variable "frontend_private_ip_address_allocation" {
7071
description = "(Optional) Frontend ip allocation type (Static or Dynamic)"
7172
default = "Dynamic"
7273
}
74+
75+
variable "lb_sku" {
76+
description = "(Optional) The SKU of the Azure Load Balancer. Accepted values are Basic and Standard."
77+
default = "Basic"
78+
}
79+
80+
variable "lb_probe" {
81+
description = "(Optional) Protocols to be used for lb health probes. Format as [protocol, port, request_path]"
82+
type = map(any)
83+
default = {}
84+
}

0 commit comments

Comments
 (0)