Skip to content

Commit

Permalink
index generated_storage_account_names as key-value pair
Browse files Browse the repository at this point in the history
  • Loading branch information
jksprattler committed Nov 27, 2024
1 parent 560cdba commit 3a4985b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cloud_Azure/terraform/module/storage_account.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# 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 name in local._alphanum_lowercase_names : substr(name, 0, 24)]
_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
}
}

# Creates one storage account per nsg per resource group to store flow logs
Expand All @@ -14,7 +18,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
name = local.generated_storage_account_names["sa${each.key}"]
resource_group_name = each.value.rg
location = var.location
account_tier = "Standard"
Expand Down

0 comments on commit 3a4985b

Please sign in to comment.