-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Azure LoadBalancer Terraform Module
- Loading branch information
0 parents
commit 10b93a8
Showing
6 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Variable files | ||
terraform.tfvars | ||
|
||
### https://raw.github.com/github/gitignore/abad92dac5a4306f72242dae3bca6e277bce3615/Terraform.gitignore | ||
|
||
# Compiled files | ||
*.tfstate | ||
*.tfstate.backup | ||
|
||
# Module directory | ||
.terraform/ | ||
|
||
|
||
### https://raw.github.com/github/gitignore/abad92dac5a4306f72242dae3bca6e277bce3615/Global/Vim.gitignore | ||
|
||
# swap | ||
[._]*.s[a-w][a-z] | ||
[._]s[a-w][a-z] | ||
# session | ||
Session.vim | ||
# temporary | ||
.netrwhist | ||
*~ | ||
# auto-generated tag files | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
Azure Load Balancer Terraform Module | ||
========== | ||
|
||
A terraform module to provide load balancers in Azure. | ||
|
||
Public or Private load balancer. Works for Azure Public and Private clouds. | ||
|
||
Usage | ||
----- | ||
Public loadbalancer example: | ||
|
||
```hcl | ||
resource "azurerm_resource_group" "rg" { | ||
name = "${var.resource_group_name}" | ||
location = "westus" | ||
tags = { | ||
environment = "nonprod" | ||
costcenter = "12345" | ||
appname = "myapp" | ||
} | ||
} | ||
module "mylb" { | ||
source = "github.com/highwayoflife/terraform-azure-loadbalancer" | ||
type = "Public" | ||
resource_group_name = "${azurerm_resource_group.rg.name}" | ||
prefix = "terraform-test" | ||
"remote_port" { | ||
ssh = ["Tcp", "22"] | ||
} | ||
"lb_port" { | ||
http = ["443", "Tcp", "443"] | ||
} | ||
} | ||
``` | ||
|
||
Private loadbalancer example: | ||
|
||
```hcl | ||
resource "azurerm_resource_group" "rg" { | ||
name = "${var.resource_group_name}" | ||
location = "westus" | ||
tags = { | ||
environment = "nonprod" | ||
costcenter = "12345" | ||
appname = "myapp" | ||
} | ||
} | ||
data "azurerm_subnet" "subnet" { | ||
name = "${var.subnet_name}" | ||
virtual_network_name = "${var.vnet_name}" | ||
resource_group_name = "${var.vnet_resource_group_name}" | ||
} | ||
module "mylb" { | ||
source = "github.com/highwayoflife/terraform-azure-loadbalancer" | ||
type = "private" | ||
prefix = "MyTerraformLB" | ||
resource_group_name = "${azurerm_resource_group.rg.name}" | ||
subnet_id = "${data.azurerm_subnet.subnet.id}" | ||
# Define a static IP, if empty or not defined, IP will be dynamic | ||
private_ip_address = "10.0.1.6" | ||
"remote_port" { | ||
ssh = ["Tcp", "22"] | ||
} | ||
"lb_port" { | ||
https = ["443", "Tcp", "443"] | ||
} | ||
} | ||
``` | ||
|
||
|
||
Argument Reference | ||
-------- | ||
The following arguments are supported: | ||
|
||
* `location` | ||
* (Optional) The location/region where the load balancer will be created. If not provided, will use the location of the resource group. | ||
* `resource_group_name` | ||
* (Required) The name of the existing resource group where the load balancer resources will be placed. | ||
* `prefix` | ||
* (Required) Default prefix to use with your resource names. | ||
* `remote_port` | ||
* Protocols to be used for remote vm access. [protocol, backend\_port]. Frontend port will be automatically generated starting at 50000 and in the output. | ||
* `lb_port` | ||
* Protocols to be used for lb health probes and rules. [frontend\_port, protocol, backend\_port] | ||
* `lb_probe_unhealthy_threshold` | ||
* (Optional) Number of times the load balancer health probe has an unsuccessful attempt before considering the endpoint unhealthy. default = 2 | ||
* `lb_probe_interval` | ||
* (Optional) Interval in seconds the load balancer health probe rule does a check. default = 5 | ||
* `frontend_name` | ||
* (Optional) Specifies the name of the frontend ip configuration. default = "FrontendIP" | ||
* `public_ip_address_allocation` | ||
* (Optional) Defines now an IP address is assigned. Options are Static or Dynamic. Default = static. | ||
* `tags` | ||
* (Optional) Map of tags in addition to tags assigned to the resource group. | ||
* `type` | ||
* (Optional) Defined if the loadbalancer is private or public. Default private. | ||
* `subnet_id` | ||
* (Optional) Frotend subnet id to use when in private mode. | ||
* `private_ip_address` | ||
* (Optional) Private ip address to assign to frontend. Use with type = private. | ||
* `private_ip_address_allocation` | ||
* (Optional) Frotend ip allocation type (Static or Dynamic). Default Dynamic. | ||
|
||
Attribute Reference | ||
------- | ||
The following attributes are exported: | ||
|
||
* `lb_id` | ||
* The id for the azurerm\_lb resource. | ||
* `lb_frontend_ip_configuration` | ||
* The frontend\_ip\_configuration for the azurerm\_lb resource | ||
* `lb_probe_ids` | ||
* The ids for the azurerm\_lb\_probe resources. | ||
* `lb_nat_rule_ids` | ||
* The ids for the azurerm\_lb\_nat\_rule resources. | ||
* `public_ip_id` | ||
* The id for the azurerm\_lb\_public\_ip\_resource. | ||
* `public_ip_address` | ||
* The ip address for the azurerm\_lb\_public\_ip resource. | ||
* `lb_backend_address_pool_id` | ||
* The id for the azurerm\_lb\_backend\_address\_pool resource | ||
* `lb_private_ip_address` | ||
* The first private IP address assigned to the load balancer in frontend\_ip\_configuration blocks, if any. | ||
|
||
Contributors | ||
----- | ||
|
||
* [David Lewis](https://github.com/highwayoflife) | ||
|
||
License | ||
------ | ||
|
||
[MIT](LICENSE) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Azure load balancer module | ||
data "azurerm_resource_group" "azlb" { | ||
name = "${var.resource_group_name}" | ||
} | ||
|
||
locals { | ||
location = "${var.location != "" ? var.location : data.azurerm_resource_group.azlb.location}" | ||
resource_group_name = "${data.azurerm_resource_group.azlb.name}" | ||
type = "${lower(var.type)}" | ||
private_ip_allocation = "${var.private_ip_address != "" ? "static" : "dynamic"}" | ||
tags = "${merge( | ||
data.azurerm_resource_group.azlb.tags, | ||
var.tags | ||
)}" | ||
} | ||
|
||
resource "azurerm_public_ip" "azlb" { | ||
count = "${local.type == "public" ? 1 : 0}" | ||
name = "${var.prefix}-publicIP" | ||
location = "${local.location}" | ||
resource_group_name = "${local.resource_group_name}" | ||
public_ip_address_allocation = "${var.public_ip_address_allocation}" | ||
tags = "${local.tags}" | ||
} | ||
|
||
resource "azurerm_lb" "azlb" { | ||
name = "${var.prefix}-lb" | ||
resource_group_name = "${local.resource_group_name}" | ||
location = "${local.location}" | ||
tags = "${local.tags}" | ||
|
||
frontend_ip_configuration { | ||
name = "${var.frontend_name}" | ||
public_ip_address_id = "${local.type == "public" ? join("",azurerm_public_ip.azlb.*.id) : ""}" | ||
subnet_id = "${var.subnet_id}" | ||
private_ip_address = "${var.private_ip_address}" | ||
private_ip_address_allocation = "${local.private_ip_allocation}" | ||
} | ||
} | ||
|
||
resource "azurerm_lb_backend_address_pool" "azlb" { | ||
resource_group_name = "${local.resource_group_name}" | ||
loadbalancer_id = "${azurerm_lb.azlb.id}" | ||
name = "BackEndAddressPool" | ||
} | ||
|
||
resource "azurerm_lb_nat_rule" "azlb" { | ||
count = "${length(var.remote_port)}" | ||
resource_group_name = "${local.resource_group_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 = "${local.resource_group_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 = "${local.resource_group_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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
output "lb_id" { | ||
description = "The id for the azurerm_lb resource" | ||
value = "${azurerm_lb.azlb.id}" | ||
} | ||
|
||
output "lb_frontend_ip_configuration" { | ||
description = "The frontend_ip_configuration for the azurerm_lb resource" | ||
value = "${azurerm_lb.azlb.frontend_ip_configuration}" | ||
} | ||
|
||
output "lb_probe_ids" { | ||
description = "The ids for the azurerm_lb_probe resources" | ||
value = "${azurerm_lb_probe.azlb.*.id}" | ||
} | ||
|
||
output "lb_nat_rule_ids" { | ||
description = "The ids for the azurerm_lb_nat_rule resources" | ||
value = "${azurerm_lb_nat_rule.azlb.*.id}" | ||
} | ||
|
||
output "public_ip_id" { | ||
description = "The id for the azurerm_lb_public_ip resource" | ||
value = "${azurerm_public_ip.azlb.*.id}" | ||
} | ||
|
||
output "public_ip_address" { | ||
description = "The ip address for the azurerm_lb_public_ip resource" | ||
value = "${azurerm_public_ip.azlb.*.ip_address}" | ||
} | ||
|
||
output "lb_backend_address_pool_id" { | ||
description = "The id for the azurerm_lb_backend_address_pool resource" | ||
value = "${azurerm_lb_backend_address_pool.azlb.id}" | ||
} | ||
|
||
output "lb_private_ip_address" { | ||
description = "The first private IP address assigned to the load balancer in frontend_ip_configuration blocks, if any." | ||
value = "${azurerm_lb.azlb.private_ip_address}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
variable "location" { | ||
description = "(Optional) The location/region where the load balancer will be created. If not provided, will use the location of the resource group." | ||
default = "" | ||
} | ||
|
||
variable "resource_group_name" { | ||
description = "(Required) The name of the existing resource group where the load balancer resources will be placed." | ||
} | ||
|
||
variable "prefix" { | ||
description = "(Required) Default prefix to use with your resource names." | ||
} | ||
|
||
variable "remote_port" { | ||
description = "Protocols to be used for remote vm access. [protocol, backend_port]. Frontend port will be automatically generated starting at 50000 and in the output." | ||
default = {} | ||
} | ||
|
||
variable "lb_port" { | ||
description = "Protocols to be used for lb health probes and rules. [frontend_port, protocol, backend_port]" | ||
default = {} | ||
} | ||
|
||
variable "lb_probe_unhealthy_threshold" { | ||
description = "Number of times the load balancer health probe has an unsuccessful attempt before considering the endpoint unhealthy." | ||
default = 2 | ||
} | ||
|
||
variable "lb_probe_interval" { | ||
description = "Interval in seconds the load balancer health probe rule does a check" | ||
default = 5 | ||
} | ||
|
||
variable "frontend_name" { | ||
description = "(Optional) Specifies the name of the frontend ip configuration." | ||
default = "FrontendIP" | ||
} | ||
|
||
variable "public_ip_address_allocation" { | ||
description = "(Optional) Defines how an IP address is assigned. Options are Static or Dynamic. Default static." | ||
default = "static" | ||
} | ||
|
||
variable "tags" { | ||
type = "map" | ||
description = "(optional) Tags to use in addition to tags assigned to the resource group." | ||
|
||
default = { | ||
source = "terraform" | ||
} | ||
} | ||
|
||
variable "type" { | ||
type = "string" | ||
description = "(Optional) Defined if the loadbalancer is private or public. Default private." | ||
default = "private" | ||
} | ||
|
||
variable "subnet_id" { | ||
description = "(Optional) Frontend subnet id to use when in private mode" | ||
default = "" | ||
} | ||
|
||
variable "private_ip_address" { | ||
description = "(Optional) Private ip address to assign to frontend. Use it with type = private" | ||
default = "" | ||
} | ||
|