Terraform如何遍历目录集结合Azure AD查询生成ADLS Gen2 ACE块
Terraform实现ADLS Gen2目录绑定Azure AD组ACL的方案
该需求技术上完全可实现,以下是最优实现方式:
实现逻辑说明
核心是通过同一个目录与AAD组的关联映射,同时遍历查询Azure AD组ID和创建目录,保证两者的对应关系完全一致,避免配置错位。
完整可运行代码示例
# 定义目录与Azure AD组的关联映射,可根据实际需求扩展 variable "dir_group_assoc" { type = map(string) description = "Key为要创建的ADLS Gen2目录名,Value为对应的Azure AD组显示名" default = { "folder_name_1" = "Azure AD Group 1", "folder_name_2" = "Azure AD Group 2" } } # 遍历关联映射,批量查询对应Azure AD组的对象ID data "azuread_group" "dir_assoc_group" { for_each = var.dir_group_assoc display_name = each.value security_enabled = true # 过滤仅匹配安全组,避免匹配到其他类型组 } # 遍历关联映射,批量创建ADLS Gen2目录并配置ACL resource "azurerm_storage_data_lake_gen2_path" "sftp_root_subdirs" { for_each = var.dir_group_assoc path = each.key filesystem_name = azurerm_storage_data_lake_gen2_filesystem.sftp_root.name storage_account_id = azurerm_storage_account.working_storage_account.id resource = "directory" # 配置当前目录的访问ACL ace { scope = "access" type = "group" permissions = "rwx" # 引用对应Azure AD组的对象ID id = data.azuread_group.dir_assoc_group[each.key].object_id } # 可选配置:如果需要目录下的子文件/子目录自动继承该ACL,新增default scope的ace块 ace { scope = "default" type = "group" permissions = "rwx" id = data.azuread_group.dir_assoc_group[each.key].object_id } }
关键注意事项
var.dir_group_assoc的键值必须唯一,避免遍历出现冲突- 确保关联映射中填写的Azure AD组显示名在当前租户内唯一,否则数据源查询会报错,也可以额外添加
mail_nickname等过滤参数缩小匹配范围 - 可根据实际业务需求调整
permissions字段的取值,支持r/w/x的任意组合 - 你原示例中引用
azurerm_client_config获取ID的写法是错误的,已修正为直接引用azuread_group数据源返回的组对象ID
内容的提问来源于stack exchange,提问作者JasonS
相关产品推荐
相关产品推荐

