Skip to content

Commit

Permalink
Correct README examples and add enhancement (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
yupwei68 authored Aug 6, 2020
1 parent b40b8fc commit da29f31
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 54 deletions.
54 changes: 34 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,80 @@ characteristics:
Public loadbalancer example:

```hcl
variable "resource_group_name" {
default = "my-terraform-lb"
provider "azurerm" {
features {}
}
variable "location" {
default = "eastus"
resource "azurerm_resource_group" "example" {
name = "example-lb"
location = "West Europe"
}
module "mylb" {
source = "Azure/loadbalancer/azurerm"
resource_group_name = "${var.resource_group_name}"
location = "${var.location}"
resource_group_name = azurerm_resource_group.example.name
prefix = "terraform-test"
"remote_port" {
remote_port = {
ssh = ["Tcp", "22"]
}
"lb_port" {
lb_port = {
http = ["80", "Tcp", "80"]
}
}
module "network" {
source = "Azure/network/azurerm"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
lb_probe = {
http = ["Tcp", "80", ""]
}
}
```

Private loadbalancer example:

```hcl
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-lb"
location = "West Europe"
}
module "mylb" {
source = "Azure/loadbalancer/azurerm"
location = "westus"
resource_group_name = azurerm_resource_group.example.name
type = "private"
frontend_subnet_id = "${module.network.vnet_subnets[0]}"
frontend_subnet_id = module.network.vnet_subnets[0]
frontend_private_ip_address_allocation = "Static"
frontend_private_ip_address = "10.0.1.6"
lb_sku = "Standard"
"remote_port" {
remote_port = {
ssh = ["Tcp", "22"]
}
"lb_port" {
lb_port = {
http = ["80", "Tcp", "80"]
https = ["443", "Tcp", "443"]
}
"tags" {
lb_probe = {
http = ["Tcp", "80", ""]
http2 = ["Http", "1443", "/"]
}
tags = {
cost-center = "12345"
source = "terraform"
}
}
module "network" {
source = "Azure/network/azurerm"
resource_group_name = "myapp"
location = "westus"
resource_group_name = azurerm_resource_group.example.name
address_space = "10.0.0.0/16"
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
subnet_names = ["subnet1", "subnet2", "subnet3"]
Expand Down
32 changes: 16 additions & 16 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# Azure load balancer module
resource "azurerm_resource_group" "azlb" {
name = var.resource_group_name
location = var.location
tags = var.tags
data "azurerm_resource_group" "azlb" {
name = var.resource_group_name
}

resource "azurerm_public_ip" "azlb" {
count = var.type == "public" ? 1 : 0
name = "${var.prefix}-publicIP"
resource_group_name = azurerm_resource_group.azlb.name
location = azurerm_resource_group.azlb.location
resource_group_name = data.azurerm_resource_group.azlb.name
location = coalesce(var.location, data.azurerm_resource_group.azlb.location)
allocation_method = var.allocation_method
tags = var.tags
}

resource "azurerm_lb" "azlb" {
name = "${var.prefix}-lb"
resource_group_name = azurerm_resource_group.azlb.name
location = azurerm_resource_group.azlb.location
resource_group_name = data.azurerm_resource_group.azlb.name
location = coalesce(var.location, data.azurerm_resource_group.azlb.location)
sku = var.lb_sku
tags = var.tags

frontend_ip_configuration {
Expand All @@ -31,14 +30,14 @@ resource "azurerm_lb" "azlb" {

resource "azurerm_lb_backend_address_pool" "azlb" {
name = "BackEndAddressPool"
resource_group_name = azurerm_resource_group.azlb.name
resource_group_name = data.azurerm_resource_group.azlb.name
loadbalancer_id = azurerm_lb.azlb.id
}

resource "azurerm_lb_nat_rule" "azlb" {
count = length(var.remote_port)
name = "VM-${count.index}"
resource_group_name = azurerm_resource_group.azlb.name
resource_group_name = data.azurerm_resource_group.azlb.name
loadbalancer_id = azurerm_lb.azlb.id
protocol = "tcp"
frontend_port = "5000${count.index + 1}"
Expand All @@ -47,20 +46,21 @@ resource "azurerm_lb_nat_rule" "azlb" {
}

resource "azurerm_lb_probe" "azlb" {
count = length(var.lb_port)
name = element(keys(var.lb_port), count.index)
resource_group_name = azurerm_resource_group.azlb.name
count = length(var.lb_probe)
name = element(keys(var.lb_probe), count.index)
resource_group_name = data.azurerm_resource_group.azlb.name
loadbalancer_id = azurerm_lb.azlb.id
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)
protocol = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 0)
port = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 1)
interval_in_seconds = var.lb_probe_interval
number_of_probes = var.lb_probe_unhealthy_threshold
request_path = element(var.lb_probe[element(keys(var.lb_probe), count.index)], 2)
}

resource "azurerm_lb_rule" "azlb" {
count = length(var.lb_port)
name = element(keys(var.lb_port), count.index)
resource_group_name = azurerm_resource_group.azlb.name
resource_group_name = data.azurerm_resource_group.azlb.name
loadbalancer_id = azurerm_lb.azlb.id
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)
Expand Down
18 changes: 9 additions & 9 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
output "azurerm_resource_group_tags" {
description = "the tags provided for the resource group"
value = "${azurerm_resource_group.azlb.tags}"
value = data.azurerm_resource_group.azlb.tags
}

output "azurerm_resource_group_name" {
description = "name of the resource group provisioned"
value = "${azurerm_resource_group.azlb.name}"
value = data.azurerm_resource_group.azlb.name
}

output "azurerm_lb_id" {
description = "the id for the azurerm_lb resource"
value = "${azurerm_lb.azlb.id}"
value = azurerm_lb.azlb.id
}

output "azurerm_lb_frontend_ip_configuration" {
description = "the frontend_ip_configuration for the azurerm_lb resource"
value = "${azurerm_lb.azlb.frontend_ip_configuration}"
value = azurerm_lb.azlb.frontend_ip_configuration
}

output "azurerm_lb_probe_ids" {
description = "the ids for the azurerm_lb_probe resources"
value = "${azurerm_lb_probe.azlb.*.id}"
value = azurerm_lb_probe.azlb.*.id
}

output "azurerm_lb_nat_rule_ids" {
description = "the ids for the azurerm_lb_nat_rule resources"
value = "${azurerm_lb_nat_rule.azlb.*.id}"
value = azurerm_lb_nat_rule.azlb.*.id
}

output "azurerm_public_ip_id" {
description = "the id for the azurerm_lb_public_ip resource"
value = "${azurerm_public_ip.azlb.*.id}"
value = azurerm_public_ip.azlb.*.id
}

output "azurerm_public_ip_address" {
description = "the ip address for the azurerm_lb_public_ip resource"
value = "${azurerm_public_ip.azlb.*.ip_address}"
value = azurerm_public_ip.azlb.*.ip_address
}

output "azurerm_lb_backend_address_pool_id" {
description = "the id for the azurerm_lb_backend_address_pool resource"
value = "${azurerm_lb_backend_address_pool.azlb.id}"
value = azurerm_lb_backend_address_pool.azlb.id
}
42 changes: 37 additions & 5 deletions test/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,49 @@ resource "random_id" "rg_name" {
byte_length = 8
}

resource "azurerm_resource_group" "test" {
name = "example-lb-${random_id.rg_name.hex}"
location = "West Europe"
}

module "mylb" {
source = "../../"
resource_group_name = "${random_id.rg_name.hex}"
location = "${var.location}"
prefix = "${random_id.rg_name.hex}"
source = "../.."
resource_group_name = azurerm_resource_group.test.name
type = "private"
frontend_subnet_id = module.network.vnet_subnets[0]
frontend_private_ip_address_allocation = "Static"
frontend_private_ip_address = "10.0.1.6"
lb_sku = "Standard"

remote_port = {
ssh = ["Tcp", "22"]
}

lb_port = {
http = ["80", "Tcp", "80"]
http = ["80", "Tcp", "80"]
https = ["443", "Tcp", "443"]
}

lb_probe = {
http = ["Tcp", "80", ""]
http2 = ["Http", "1443", "/"]
}

tags = {
cost-center = "12345"
source = "terraform"
}
}

module "network" {
source = "Azure/network/azurerm"
resource_group_name = azurerm_resource_group.test.name
address_space = "10.0.0.0/16"
subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
subnet_names = ["subnet1", "subnet2", "subnet3"]

tags = {
environment = "dev"
costcenter = "it"
}
}
20 changes: 16 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
variable "location" {
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"
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"
default = ""
}

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

variable "prefix" {
Expand All @@ -18,7 +18,8 @@ variable "remote_port" {
}

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

Expand Down Expand Up @@ -70,3 +71,14 @@ variable "frontend_private_ip_address_allocation" {
description = "(Optional) Frontend ip allocation type (Static or Dynamic)"
default = "Dynamic"
}

variable "lb_sku" {
description = "(Optional) The SKU of the Azure Load Balancer. Accepted values are Basic and Standard."
default = "Basic"
}

variable "lb_probe" {
description = "(Optional) Protocols to be used for lb health probes. Format as [protocol, port, request_path]"
type = map(any)
default = {}
}

0 comments on commit da29f31

Please sign in to comment.