Using Terraform, I have a list of maps defined as variable eg
storage_accounts = {
stacctest1 = {
resource_group_name = "testrg",
location = "uksouth",
account_tier = "Standard",
account_replication_type = "GRS",
containers_list = [
{ name = "test_private_x", access_type = "private" },
{ name = "test_blob_x", access_type = "blob" },
{ name = "test_container_x", access_type = "container" }
]
}
stacctest2 = {
resource_group_name = "testrg",
location = "uksouth",
account_tier = "Standard",
account_replication_type = "GRS",
containers_list = [
{ name = "test_private_a", access_type = "private" },
{ name = "test_blob_a", access_type = "blob" },
{ name = "test_container_a", access_type = "container" }
]
}
}
Then in a module, I can use a for_each loop to go through each item in the list to create each storage account, eg
resource "azurerm_storage_account" "storage" {
for_each = var.storage_accounts
name = each.key
resource_group_name = each.value.resource_group_name
location = each.value.location
account_tier = each.value.account_tier
account_replication_type = each.value.account_replication_type
}
}
To process the container sequence in the map, I was thinking I can just loop though the list again process the containers, but struggling to work out how to fit a nested loop in with resource "azurerm_storage_container"
You have to flatten your variable. For example:
locals {
flat_storage_accounts = merge([
for account_name, details in var.storage_accounts: {
for container in details.containers_list:
"${account_name}-${container.name}" =>
merge(details, {
"account_name" = account_name
"container" = container
})
}
]...)
}
then
resource "azurerm_storage_account" "storage" {
for_each = local.flat_storage_accounts
name = each.account_name
resource_group_name = each.value.resource_group_name
location = each.value.location
account_tier = each.value.account_tier
account_replication_type = each.value.account_replication_type
# use individual container_list as
# some_attribute = each.value.container.access_type
}
You haven't provided code where you want to use containers_list
, so I can only make comment above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.