diff --git a/README.md b/README.md index 3029eea..52fd3f7 100644 --- a/README.md +++ b/README.md @@ -15,57 +15,72 @@ 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" } @@ -73,8 +88,7 @@ module "mylb" { 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"] diff --git a/main.tf b/main.tf index 8233966..6c53b52 100644 --- a/main.tf +++ b/main.tf @@ -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 { @@ -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}" @@ -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) diff --git a/outputs.tf b/outputs.tf index 55def1f..6c7fb6c 100644 --- a/outputs.tf +++ b/outputs.tf @@ -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 } diff --git a/test/fixture/main.tf b/test/fixture/main.tf index 3d93df4..9aae499 100644 --- a/test/fixture/main.tf +++ b/test/fixture/main.tf @@ -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" } } diff --git a/variables.tf b/variables.tf index a53e67d..056a332 100644 --- a/variables.tf +++ b/variables.tf @@ -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" { @@ -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 = {} } @@ -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 = {} +}