Skip to content

Commit

Permalink
map sa name to nsg key
Browse files Browse the repository at this point in the history
  • Loading branch information
jksprattler committed Nov 27, 2024
1 parent 3a4985b commit 3da4473
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions cloud_Azure/terraform/module/storage_account.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Prepare names that meet Azure Storage Account naming restrictions (only alphanum letters, max 24 length, Azure-wide unique)
# Each output name is concatenation of Resource Group name and Subscription ID, adjusted to naming restrictions
# Maps each storage account name to a key-value pair indexed by sa0, sa1, etc
locals {
_names = [for name in var.resource_group_names : "${name}${var.subscription_id}"]
_lowercase_names = [for name in local._names : lower(name)]
_alphanum_lowercase_names = [for name in local._lowercase_names : join("", regexall("[[:alnum:]]+", name))]
generated_storage_account_names = {
for i, name in local._alphanum_lowercase_names :
"sa${i}" => substr(name, 0, 24) # Use the index as a key for sa creation
}
_names = [for name in var.resource_group_names : "${name}${var.subscription_id}"]
_lowercase_names = [for name in local._names : lower(name)]
_alphanum_lowercase_names = [for name in local._lowercase_names : join("", regexall("[[:alnum:]]+", name))]
generated_storage_account_names = [for i, name in local._alphanum_lowercase_names : substr(name, 0, 24)]
}

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

# generate storage account per nsg(s) in each rg
name = local.generated_storage_account_names["sa${each.key}"]
name = local.generated_storage_account_names[lookup(local.flat_nsgs, each.key, 0)]
resource_group_name = each.value.rg
location = var.location
account_tier = "Standard"
Expand Down

0 comments on commit 3da4473

Please sign in to comment.