You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Terraform的Count而非For_each创建3个存储账户各含3个容器

实现每个存储账户下创建3个容器(基于Count+For表达式)

我正在学习Terraform,现有代码可创建3个各含1个容器的存储账户,现需修改代码,不使用for_each,仅通过Count结合For表达式实现每个存储账户下创建3个容器。

现有代码

1. main.tf

# 创建资源组
resource "azurerm_resource_group" "sam_rg" {
  name     = var.resource_group_name
  location = var.location
  tags     = local.tags
}

# 使用count创建3个存储账户
resource "azurerm_storage_account" "sampath-storage-accounts" {
  count                     = length(var.storage-account_config)
  name                      = "${count.index}${keys(var.storage-account_config)[count.index]}${lower(var.Environment)}"
  location                  = azurerm_resource_group.sam_rg.location
  resource_group_name       = azurerm_resource_group.sam_rg.name
  account_tier              = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_tier
  account_kind              = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_kind
  account_replication_type  = var.storage-account_config[keys(var.storage-account_config)[count.index]].account_replication_type
  access_tier               = var.storage-account_config[keys(var.storage-account_config)[count.index]].access_tier
  shared_access_key_enabled = var.storage-account_config[keys(var.storage-account_config)[count.index]].shared_access_key_enabled
  min_tls_version           = var.storage-account_config[keys(var.storage-account_config)[count.index]].min_tls_version
  depends_on = [
    azurerm_resource_group.sam_rg
  ]
  tags = local.tags
}

# 当前仅创建3个容器(每个存储账户对应1个)
resource "azurerm_storage_container" "sampath-containers" {
  count                 = length(var.storage_container-config)
  name                  = keys(var.storage_container-config)[count.index]
  storage_account_name  = azurerm_storage_account.sampath-storage-accounts[count.index].name
  container_access_type = var.storage_container-config[keys(var.storage_container-config)[count.index]].container_access_type
}

2. local.tf

locals {
  tags = {
    "Environment" = "var.Environment" # 注意:此处应去掉引号,改为var.Environment
    "Project"     = "var.Project"      # 注意:此处应去掉引号,改为var.Project
    "Team"        = "Compliance"
    "Location"    = "data.azurerm_resource_group.sam-rg.location" # 注意:若使用资源组数据源需先定义,否则直接用var.location
  }
}

3. variable.tf

variable "resource_group_name" {
  type        = string
  description = "已存在的资源组名称" # 修正原拼写错误
}

variable "location" {
  type = string
}

variable "Environment" {
  type        = string
  description = "资源所属环境"
}
variable "Project" {
  type        = string
  description = "资源所属项目"
}

variable "storage-account_config" {
  type = map(object({
    account_tier              = string
    account_kind              = string
    account_replication_type  = string
    access_tier               = string
    shared_access_key_enabled = bool
    min_tls_version           = string
  }))
  description = "存储账户配置映射"
}

variable "storage_container-config" {
  type = map(object({
    container_access_type = string
  }))
  description = "存储容器配置映射"
}

4. variable.tfvars

resource_group_name = "abp-rg-env_dev"

location = "eastus"

Environment = "Development"

Project = "ABP-01"

storage-account_config = {
    sampath = {        
        account_tier              = "Standard"
        account_kind              = "StorageV2"
        account_replication_type  = "LRS"
        access_tier               = "Hot"
        shared_access_key_enabled = true # 修正:原字符串"true"改为布尔值true
        min_tls_version           = "TLS1_2"
    }
    sandeep = {
        account_tier              = "Premium"
        account_kind              = "StorageV2"
        account_replication_type  = "GRS"
        access_tier               = "Cool"
        shared_access_key_enabled = false
        min_tls_version           = "TLS1_2"
    }
    aravind = {
        account_tier              = "Standard"
        account_kind              = "StorageV2"
        account_replication_type  = "ZRS"
        access_tier               = "Cool"
        shared_access_key_enabled = false
        min_tls_version           = "TLS1_2"
    }
}

storage_container-config = {
    container01 = {       
        container_access_type = "private"
    }
    container02 = {        
        container_access_type = "blob"
    }
    container03 = {        
        container_access_type = "private"
    }
}

修改方案(Count+For表达式实现笛卡尔积)

步骤1:添加本地值生成存储账户与容器的组合

在local.tf中添加一个本地值,通过For表达式生成存储账户×容器的笛卡尔积组合,将嵌套列表展开为一维列表:

locals {
  tags = {
    Environment = var.Environment
    Project     = var.Project
    Team        = "Compliance"
    Location    = var.location
  }

  # 生成存储账户与容器的笛卡尔积组合,每个元素包含存储账户索引、容器名称及配置
  storage_container_pairs = flatten([
    for sa_idx, sa_key in keys(var.storage-account_config) : [
      for cnt_key, cnt_config in var.storage_container-config : {
        sa_index    = sa_idx
        cnt_name    = cnt_key
        cnt_config  = cnt_config
      }
    ]
  ])
}

步骤2:修改存储容器资源块

将main.tf中的存储容器资源块替换为以下代码,基于上述组合列表的长度设置count,并通过索引关联对应的存储账户和容器:

resource "azurerm_storage_container" "sampath-containers" {
  # 总实例数 = 存储账户数 × 容器数 = 3×3=9
  count = length(local.storage_container_pairs)

  # 从组合列表中获取当前容器名称
  name                  = local.storage_container_pairs[count.index].cnt_name
  # 关联对应的存储账户(通过组合列表中的存储账户索引)
  storage_account_name  = azurerm_storage_account.sampath-storage-accounts[local.storage_container_pairs[count.index].sa_index].name
  # 获取容器的访问权限配置
  container_access_type = local.storage_container_pairs[count.index].cnt_config.container_access_type
}

说明

  • 该方案通过flatten+嵌套For表达式生成所有存储账户与容器的组合,确保每个存储账户下创建3个指定容器
  • 无需使用for_each,完全基于count实现需求
  • 同时修正了原代码中的若干语法错误(如布尔值引用、变量引用格式)

内容的提问来源于stack exchange,提问作者Sampath Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 09:57:22