具有多个列表的 for 循环

for loop with multiple lists

我在 terraform 中有 2 个变量列表。需要同时使用这两个列表并创建资源

我有什么

        locals {
        bucket_name = ["SRE", "Engg", "QA"]
        access_type =["Private", "Public" ]
        }
        
         resource "oci_objectstorage_bucket" "test_bucket" {
    for_each = local.bucket_name 
        compartment_id = var.compartment_id
        name = each.value
        namespace = var.bucket_namespace
        access_type = "Private"  ## for Private 
    }

resource "oci_objectstorage_bucket" "test_bucket" {
    for_each = local.bucket_name 
        compartment_id = var.compartment_id
        name = each.value
        namespace = var.bucket_namespace
        access_type = "Public"   ## For Public 
    }

有了上面的资源块,我就可以创建所需的桶了。但是,我不想对同一组代码(一个用于私有,另一个用于 public)使用 2 个块。是否有可能合并 2 个列表并创建资源

是的,您可以使用 setproduct 遍历 bucket_nameaccess_type:

locals {
  bucket_name = ["SRE", "Engg", "QA"]
  access_type = ["Private", "Public" ]
  
  my_product = {for val in setproduct(local.bucket_name, local.access_type):
                "${val[0]}-${val[1]}" => val}  
}

然后

resource "oci_objectstorage_bucket" "test_bucket" {
        for_each = local.my_product 
        compartment_id = var.compartment_id
        name = each.value[0]
        namespace = var.bucket_namespace
        access_type = each.value[1]
}