Skip to content

Commit 8946fa5

Browse files
committed
feat: adding Azure VMSS Building Block module
1 parent 2008030 commit 8946fa5

File tree

9 files changed

+2146
-0
lines changed

9 files changed

+2146
-0
lines changed

modules/azure/vmss/buildingblock/APP_TEAM_README.md

Lines changed: 542 additions & 0 deletions
Large diffs are not rendered by default.

modules/azure/vmss/buildingblock/README.md

Lines changed: 399 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
data "azurerm_subscription" "current" {}
2+
3+
data "azurerm_client_config" "current" {}
4+
5+
data "azurerm_virtual_network" "spoke_vnet" {
6+
name = var.vnet_name
7+
resource_group_name = var.vnet_resource_group_name
8+
}
9+
10+
data "azurerm_subnet" "vmss_subnet" {
11+
name = var.subnet_name
12+
virtual_network_name = var.vnet_name
13+
resource_group_name = var.vnet_resource_group_name
14+
}
15+
16+
resource "random_string" "resource_code" {
17+
length = 5
18+
special = false
19+
upper = false
20+
}
21+
22+
resource "azurerm_resource_group" "vmss_rg" {
23+
name = var.resource_group_name
24+
location = var.location
25+
tags = var.tags
26+
}
27+
28+
resource "azurerm_network_security_group" "vmss_nsg" {
29+
name = "${var.vmss_name}-nsg"
30+
location = azurerm_resource_group.vmss_rg.location
31+
resource_group_name = azurerm_resource_group.vmss_rg.name
32+
33+
tags = var.tags
34+
}
35+
36+
resource "azurerm_network_security_rule" "allow_ssh" {
37+
count = var.os_type == "Linux" && var.enable_ssh_access ? 1 : 0
38+
name = "AllowSSH"
39+
priority = 1000
40+
direction = "Inbound"
41+
access = "Allow"
42+
protocol = "Tcp"
43+
source_port_range = "*"
44+
destination_port_range = "22"
45+
source_address_prefix = "*"
46+
destination_address_prefix = "*"
47+
resource_group_name = azurerm_resource_group.vmss_rg.name
48+
network_security_group_name = azurerm_network_security_group.vmss_nsg.name
49+
}
50+
51+
resource "azurerm_network_security_rule" "allow_rdp" {
52+
count = var.os_type == "Windows" && var.enable_rdp_access ? 1 : 0
53+
name = "AllowRDP"
54+
priority = 1001
55+
direction = "Inbound"
56+
access = "Allow"
57+
protocol = "Tcp"
58+
source_port_range = "*"
59+
destination_port_range = "3389"
60+
source_address_prefix = "*"
61+
destination_address_prefix = "*"
62+
resource_group_name = azurerm_resource_group.vmss_rg.name
63+
network_security_group_name = azurerm_network_security_group.vmss_nsg.name
64+
}
65+
66+
resource "azurerm_network_security_rule" "allow_backend_port" {
67+
count = var.enable_load_balancer ? 1 : 0
68+
name = "AllowBackendPort"
69+
priority = 1002
70+
direction = "Inbound"
71+
access = "Allow"
72+
protocol = "Tcp"
73+
source_port_range = "*"
74+
destination_port_range = var.backend_port
75+
source_address_prefix = "*"
76+
destination_address_prefix = "*"
77+
resource_group_name = azurerm_resource_group.vmss_rg.name
78+
network_security_group_name = azurerm_network_security_group.vmss_nsg.name
79+
}
80+
81+
resource "azurerm_subnet_network_security_group_association" "vmss_nsg_association" {
82+
subnet_id = data.azurerm_subnet.vmss_subnet.id
83+
network_security_group_id = azurerm_network_security_group.vmss_nsg.id
84+
}
85+
86+
resource "azurerm_public_ip" "lb_public_ip" {
87+
count = var.enable_load_balancer && var.enable_public_ip ? 1 : 0
88+
name = "${var.vmss_name}-lb-pip"
89+
location = azurerm_resource_group.vmss_rg.location
90+
resource_group_name = azurerm_resource_group.vmss_rg.name
91+
allocation_method = "Static"
92+
sku = var.load_balancer_sku
93+
94+
tags = var.tags
95+
}
96+
97+
resource "azurerm_lb" "vmss_lb" {
98+
count = var.enable_load_balancer ? 1 : 0
99+
name = "${var.vmss_name}-lb"
100+
location = azurerm_resource_group.vmss_rg.location
101+
resource_group_name = azurerm_resource_group.vmss_rg.name
102+
sku = var.load_balancer_sku
103+
104+
frontend_ip_configuration {
105+
name = "PublicIPAddress"
106+
public_ip_address_id = var.enable_public_ip ? azurerm_public_ip.lb_public_ip[0].id : null
107+
subnet_id = var.enable_public_ip ? null : data.azurerm_subnet.vmss_subnet.id
108+
private_ip_address_allocation = var.enable_public_ip ? null : "Dynamic"
109+
}
110+
111+
tags = var.tags
112+
}
113+
114+
resource "azurerm_lb_backend_address_pool" "vmss_backend_pool" {
115+
count = var.enable_load_balancer ? 1 : 0
116+
loadbalancer_id = azurerm_lb.vmss_lb[0].id
117+
name = "BackEndAddressPool"
118+
}
119+
120+
resource "azurerm_lb_probe" "vmss_health_probe" {
121+
count = var.enable_load_balancer ? 1 : 0
122+
loadbalancer_id = azurerm_lb.vmss_lb[0].id
123+
name = "health-probe"
124+
protocol = var.health_probe_protocol
125+
port = var.health_probe_port
126+
request_path = var.health_probe_protocol != "Tcp" ? var.health_probe_request_path : null
127+
interval_in_seconds = 15
128+
number_of_probes = 2
129+
}
130+
131+
resource "azurerm_lb_rule" "vmss_lb_rule" {
132+
count = var.enable_load_balancer ? 1 : 0
133+
loadbalancer_id = azurerm_lb.vmss_lb[0].id
134+
name = "LBRule"
135+
protocol = "Tcp"
136+
frontend_port = var.frontend_port
137+
backend_port = var.backend_port
138+
frontend_ip_configuration_name = "PublicIPAddress"
139+
backend_address_pool_ids = [azurerm_lb_backend_address_pool.vmss_backend_pool[0].id]
140+
probe_id = azurerm_lb_probe.vmss_health_probe[0].id
141+
enable_floating_ip = false
142+
idle_timeout_in_minutes = 4
143+
}
144+
145+
resource "azurerm_linux_virtual_machine_scale_set" "vmss" {
146+
count = var.os_type == "Linux" ? 1 : 0
147+
name = var.vmss_name
148+
location = azurerm_resource_group.vmss_rg.location
149+
resource_group_name = azurerm_resource_group.vmss_rg.name
150+
sku = var.sku
151+
instances = var.enable_autoscaling ? null : var.instances
152+
admin_username = var.admin_username
153+
custom_data = var.custom_data != null ? base64encode(var.custom_data) : null
154+
155+
upgrade_mode = var.upgrade_mode
156+
overprovision = var.overprovision
157+
single_placement_group = var.single_placement_group
158+
zones = var.zones
159+
zone_balance = length(var.zones) > 0 ? true : false
160+
platform_fault_domain_count = var.single_placement_group ? 5 : 1
161+
priority = var.enable_spot_instances ? "Spot" : "Regular"
162+
eviction_policy = var.enable_spot_instances ? var.spot_eviction_policy : null
163+
max_bid_price = var.enable_spot_instances ? var.spot_max_bid_price : null
164+
165+
admin_ssh_key {
166+
username = var.admin_username
167+
public_key = var.ssh_public_key
168+
}
169+
170+
source_image_reference {
171+
publisher = var.image_publisher
172+
offer = var.image_offer
173+
sku = var.image_sku
174+
version = var.image_version
175+
}
176+
177+
os_disk {
178+
caching = "ReadWrite"
179+
storage_account_type = var.os_disk_storage_type
180+
disk_size_gb = var.os_disk_size_gb
181+
}
182+
183+
network_interface {
184+
name = "vmss-nic"
185+
primary = true
186+
187+
ip_configuration {
188+
name = "internal"
189+
primary = true
190+
subnet_id = data.azurerm_subnet.vmss_subnet.id
191+
load_balancer_backend_address_pool_ids = var.enable_load_balancer ? [azurerm_lb_backend_address_pool.vmss_backend_pool[0].id] : []
192+
}
193+
}
194+
195+
identity {
196+
type = "SystemAssigned"
197+
}
198+
199+
dynamic "boot_diagnostics" {
200+
for_each = var.enable_boot_diagnostics ? [1] : []
201+
content {
202+
storage_account_uri = null
203+
}
204+
}
205+
206+
dynamic "automatic_instance_repair" {
207+
for_each = var.upgrade_mode == "Automatic" || var.upgrade_mode == "Rolling" ? [1] : []
208+
content {
209+
enabled = true
210+
grace_period = "PT30M"
211+
}
212+
}
213+
214+
dynamic "rolling_upgrade_policy" {
215+
for_each = var.upgrade_mode == "Rolling" ? [1] : []
216+
content {
217+
max_batch_instance_percent = 20
218+
max_unhealthy_instance_percent = 20
219+
max_unhealthy_upgraded_instance_percent = 20
220+
pause_time_between_batches = "PT2M"
221+
}
222+
}
223+
224+
health_probe_id = var.upgrade_mode == "Automatic" || var.upgrade_mode == "Rolling" ? azurerm_lb_probe.vmss_health_probe[0].id : null
225+
226+
disable_password_authentication = true
227+
228+
tags = var.tags
229+
}
230+
231+
resource "azurerm_windows_virtual_machine_scale_set" "vmss" {
232+
count = var.os_type == "Windows" ? 1 : 0
233+
name = var.vmss_name
234+
location = azurerm_resource_group.vmss_rg.location
235+
resource_group_name = azurerm_resource_group.vmss_rg.name
236+
sku = var.sku
237+
instances = var.enable_autoscaling ? null : var.instances
238+
admin_username = var.admin_username
239+
admin_password = var.admin_password
240+
custom_data = var.custom_data != null ? base64encode(var.custom_data) : null
241+
242+
upgrade_mode = var.upgrade_mode
243+
overprovision = var.overprovision
244+
single_placement_group = var.single_placement_group
245+
zones = var.zones
246+
zone_balance = length(var.zones) > 0 ? true : false
247+
platform_fault_domain_count = var.single_placement_group ? 5 : 1
248+
priority = var.enable_spot_instances ? "Spot" : "Regular"
249+
eviction_policy = var.enable_spot_instances ? var.spot_eviction_policy : null
250+
max_bid_price = var.enable_spot_instances ? var.spot_max_bid_price : null
251+
252+
source_image_reference {
253+
publisher = var.image_publisher
254+
offer = var.image_offer
255+
sku = var.image_sku
256+
version = var.image_version
257+
}
258+
259+
os_disk {
260+
caching = "ReadWrite"
261+
storage_account_type = var.os_disk_storage_type
262+
disk_size_gb = var.os_disk_size_gb
263+
}
264+
265+
network_interface {
266+
name = "vmss-nic"
267+
primary = true
268+
269+
ip_configuration {
270+
name = "internal"
271+
primary = true
272+
subnet_id = data.azurerm_subnet.vmss_subnet.id
273+
load_balancer_backend_address_pool_ids = var.enable_load_balancer ? [azurerm_lb_backend_address_pool.vmss_backend_pool[0].id] : []
274+
}
275+
}
276+
277+
identity {
278+
type = "SystemAssigned"
279+
}
280+
281+
dynamic "boot_diagnostics" {
282+
for_each = var.enable_boot_diagnostics ? [1] : []
283+
content {
284+
storage_account_uri = null
285+
}
286+
}
287+
288+
dynamic "automatic_instance_repair" {
289+
for_each = var.upgrade_mode == "Automatic" || var.upgrade_mode == "Rolling" ? [1] : []
290+
content {
291+
enabled = true
292+
grace_period = "PT30M"
293+
}
294+
}
295+
296+
dynamic "rolling_upgrade_policy" {
297+
for_each = var.upgrade_mode == "Rolling" ? [1] : []
298+
content {
299+
max_batch_instance_percent = 20
300+
max_unhealthy_instance_percent = 20
301+
max_unhealthy_upgraded_instance_percent = 20
302+
pause_time_between_batches = "PT2M"
303+
}
304+
}
305+
306+
health_probe_id = var.upgrade_mode == "Automatic" || var.upgrade_mode == "Rolling" ? azurerm_lb_probe.vmss_health_probe[0].id : null
307+
308+
tags = var.tags
309+
}
310+
311+
resource "azurerm_monitor_autoscale_setting" "vmss_autoscale" {
312+
count = var.enable_autoscaling ? 1 : 0
313+
name = "${var.vmss_name}-autoscale"
314+
location = azurerm_resource_group.vmss_rg.location
315+
resource_group_name = azurerm_resource_group.vmss_rg.name
316+
target_resource_id = var.os_type == "Linux" ? azurerm_linux_virtual_machine_scale_set.vmss[0].id : azurerm_windows_virtual_machine_scale_set.vmss[0].id
317+
318+
profile {
319+
name = "defaultProfile"
320+
321+
capacity {
322+
default = var.instances
323+
minimum = var.min_instances
324+
maximum = var.max_instances
325+
}
326+
327+
rule {
328+
metric_trigger {
329+
metric_name = "Percentage CPU"
330+
metric_resource_id = var.os_type == "Linux" ? azurerm_linux_virtual_machine_scale_set.vmss[0].id : azurerm_windows_virtual_machine_scale_set.vmss[0].id
331+
time_grain = "PT1M"
332+
statistic = "Average"
333+
time_window = "PT5M"
334+
time_aggregation = "Average"
335+
operator = "GreaterThan"
336+
threshold = var.scale_out_cpu_threshold
337+
}
338+
339+
scale_action {
340+
direction = "Increase"
341+
type = "ChangeCount"
342+
value = "1"
343+
cooldown = "PT5M"
344+
}
345+
}
346+
347+
rule {
348+
metric_trigger {
349+
metric_name = "Percentage CPU"
350+
metric_resource_id = var.os_type == "Linux" ? azurerm_linux_virtual_machine_scale_set.vmss[0].id : azurerm_windows_virtual_machine_scale_set.vmss[0].id
351+
time_grain = "PT1M"
352+
statistic = "Average"
353+
time_window = "PT5M"
354+
time_aggregation = "Average"
355+
operator = "LessThan"
356+
threshold = var.scale_in_cpu_threshold
357+
}
358+
359+
scale_action {
360+
direction = "Decrease"
361+
type = "ChangeCount"
362+
value = "1"
363+
cooldown = "PT5M"
364+
}
365+
}
366+
}
367+
368+
tags = var.tags
369+
}

0 commit comments

Comments
 (0)