Skip to content

Commit 3da4473

Browse files
committed
map sa name to nsg key
1 parent 3a4985b commit 3da4473

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

cloud_Azure/terraform/module/storage_account.tf

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Prepare names that meet Azure Storage Account naming restrictions (only alphanum letters, max 24 length, Azure-wide unique)
22
# Each output name is concatenation of Resource Group name and Subscription ID, adjusted to naming restrictions
3-
# Maps each storage account name to a key-value pair indexed by sa0, sa1, etc
43
locals {
5-
_names = [for name in var.resource_group_names : "${name}${var.subscription_id}"]
6-
_lowercase_names = [for name in local._names : lower(name)]
7-
_alphanum_lowercase_names = [for name in local._lowercase_names : join("", regexall("[[:alnum:]]+", name))]
8-
generated_storage_account_names = {
9-
for i, name in local._alphanum_lowercase_names :
10-
"sa${i}" => substr(name, 0, 24) # Use the index as a key for sa creation
11-
}
4+
_names = [for name in var.resource_group_names : "${name}${var.subscription_id}"]
5+
_lowercase_names = [for name in local._names : lower(name)]
6+
_alphanum_lowercase_names = [for name in local._lowercase_names : join("", regexall("[[:alnum:]]+", name))]
7+
generated_storage_account_names = [for i, name in local._alphanum_lowercase_names : substr(name, 0, 24)]
128
}
139

1410
# Creates one storage account per nsg per resource group to store flow logs
@@ -18,7 +14,7 @@ resource "azurerm_storage_account" "logs_storage_account" {
1814
for_each = { for nsg in local.flat_nsgs : nsg.key => nsg.value }
1915

2016
# generate storage account per nsg(s) in each rg
21-
name = local.generated_storage_account_names["sa${each.key}"]
17+
name = local.generated_storage_account_names[lookup(local.flat_nsgs, each.key, 0)]
2218
resource_group_name = each.value.rg
2319
location = var.location
2420
account_tier = "Standard"

0 commit comments

Comments
 (0)