Skip to content

Commit e752b86

Browse files
committed
fix name logic
1 parent 28aaf1d commit e752b86

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

_locals.tf

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ locals {
1111
rg_name = var.resource_group.create ? azurerm_resource_group.this["this"].name : data.azurerm_resource_group.existing[0].name
1212
rg_loc = var.resource_group.create ? azurerm_resource_group.this["this"].location : (try(var.resource_group.location, null) != null ? var.resource_group.location : data.azurerm_resource_group.existing[0].location)
1313

14-
vm_type_prefix = local.is_linux ? "lnx" : "win"
15-
# VMSS name rules: lowercase letters, numbers, hyphen; <= 63 chars
16-
base_vmss_name_raw = "vmss-${local.vm_type_prefix}-${local.prefix}-${try(var.vmss.name_suffix, "001")}"
17-
base_vmss_name = substr(replace(lower(local.base_vmss_name_raw), "/[^0-9a-z-]/", "-"), 0, 63)
18-
vmss_name = coalesce(try(var.vmss.name, null), local.base_vmss_name)
19-
2014
is_linux = lower(var.vmss.os_type) == "linux"
2115
is_windows = lower(var.vmss.os_type) == "windows"
2216

17+
# Resource names: use var.name directly as the full name
18+
vmss_name = var.name
19+
2320
# SSH public key
2421
ssh_public_key_raw = try(var.vmss.ssh_public_key, null)
2522
ssh_public_key = try(trimspace(tostring(local.ssh_public_key_raw)), "")
@@ -43,7 +40,7 @@ locals {
4340
sig_gallery_name_raw = coalesce(try(var.gallery.gallery_name, null), "sig_${local.prefix}")
4441
sig_gallery_name = substr(replace(replace(local.sig_gallery_name_raw, "-", "_"), "/[^0-9A-Za-z._]/", "_"), 0, 80)
4542

46-
sig_image_name_raw = coalesce(try(var.gallery.image_name, null), "img_${local.prefix}_${try(var.vmss.name_suffix, "001")}")
43+
sig_image_name_raw = coalesce(try(var.gallery.image_name, null), "img_${local.vmss_name}")
4744
sig_image_name = substr(replace(replace(local.sig_image_name_raw, "-", "_"), "/[^0-9A-Za-z._]/", "_"), 0, 80)
4845

4946
sig_should_create_gallery = local.gallery_enabled && try(var.gallery.create_gallery, true)
@@ -52,11 +49,7 @@ locals {
5249
# Autoscale
5350
autoscale_enabled = try(var.autoscale.enabled, false)
5451

55-
autoscale_name = substr(
56-
replace(lower("as-${local.vmss_name}-${try(var.autoscale.name_suffix, "cpu")}"), "/[^0-9a-z-]/", "-"),
57-
0,
58-
80
59-
)
52+
autoscale_name = "as-${local.vmss_name}"
6053

6154
# Windows computer_name_prefix: max 9 chars
6255
windows_cnp_default = substr(replace(lower(local.prefix), "/[^0-9a-z]/", ""), 0, 9)
@@ -65,8 +58,8 @@ locals {
6558
# VMSS ID (used by autoscale target)
6659
vmss_id = local.is_linux ? azurerm_linux_virtual_machine_scale_set.this["this"].id : azurerm_windows_virtual_machine_scale_set.this["this"].id
6760

68-
nic_name = "nic-vmss-${local.vm_type_prefix}-${local.prefix}-${try(var.vmss.name_suffix, "001")}"
69-
pip_name = "pip-vmss-${local.vm_type_prefix}-${local.prefix}-${try(var.vmss.name_suffix, "001")}"
61+
nic_name = "nic-${local.vmss_name}"
62+
pip_name = "pip-${local.vmss_name}"
7063

7164
diag_enabled = try(var.diagnostics.enabled, false) && (try(var.diagnostics.log_analytics_workspace_id, null) != null || try(var.diagnostics.storage_account_id, null) != null || try(var.diagnostics.eventhub_authorization_rule_id, null) != null)
7265
}

_variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ variable "vmss" {
3939
type = object({
4040
os_type = string # linux | windows
4141
name = optional(string)
42-
name_suffix = optional(string, "001")
4342

4443
sku = string
4544
instances = optional(number, 1)
@@ -60,7 +59,9 @@ variable "vmss" {
6059
})
6160

6261
# Networking
63-
enable_accelerated_networking = optional(bool, false)
62+
enable_accelerated_networking = optional(bool, false)
63+
load_balancer_backend_address_pool_ids = optional(list(string))
64+
application_gateway_backend_address_pool_ids = optional(list(string))
6465

6566
# Security
6667
security_type = optional(string, "Standard") # Standard | TrustedLaunch | ConfidentialVM
@@ -94,7 +95,6 @@ variable "autoscale" {
9495
description = "Autoscale settings for the VMSS."
9596
type = object({
9697
enabled = bool
97-
name_suffix = optional(string, "cpu")
9898

9999
capacity = optional(object({
100100
min = number

main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ resource "azurerm_linux_virtual_machine_scale_set" "this" {
8383
ip_configuration {
8484
name = "ipconfig1"
8585
primary = true
86-
subnet_id = var.subnet_id
86+
subnet_id = var.subnet_id
87+
load_balancer_backend_address_pool_ids = try(each.value.load_balancer_backend_address_pool_ids, null)
88+
application_gateway_backend_address_pool_ids = try(each.value.application_gateway_backend_address_pool_ids, null)
8789
}
8890
}
8991

@@ -155,7 +157,9 @@ resource "azurerm_windows_virtual_machine_scale_set" "this" {
155157
ip_configuration {
156158
name = "ipconfig1"
157159
primary = true
158-
subnet_id = var.subnet_id
160+
subnet_id = var.subnet_id
161+
load_balancer_backend_address_pool_ids = try(each.value.load_balancer_backend_address_pool_ids, null)
162+
application_gateway_backend_address_pool_ids = try(each.value.application_gateway_backend_address_pool_ids, null)
159163
}
160164
}
161165

@@ -185,7 +189,7 @@ resource "azurerm_windows_virtual_machine_scale_set" "this" {
185189
resource "azurerm_monitor_autoscale_setting" "this" {
186190
for_each = try(var.autoscale.enabled, false) ? { "this" = true } : {}
187191

188-
name = "as-${local.vmss_name}-${try(var.autoscale.name_suffix, "cpu")}"
192+
name = local.autoscale_name
189193
resource_group_name = local.rg_name
190194
location = local.rg_loc
191195

0 commit comments

Comments
 (0)