简体   繁体   English

如何在 Terraform 的 for_each 循环内访问 map 中的序列

[英]How to access a sequence in a map inside a for_each loop in Terraform

Using Terraform, I have a list of maps defined as variable eg使用 Terraform,我有一个定义为变量的映射列表,例如

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然后在一个模块中,我可以通过列表中的每个项目使用for_each循环到go来创建每个存储帐户,例如

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"为了处理 map 中的容器序列,我想我可以通过列表再次循环处理容器,但努力解决如何使用资源“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.您没有提供要使用containers_list的代码,所以我只能在上面发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM