Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop with multiple lists

I have 2 lists of variables in terraform. Need to use both lists and create the resource

What I have

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 
}

With the above resource blocks, I can create the required buckets. However, I don't want to use 2 blocks for the same set (one for private and the other for public) of code. Is there any possibility to combine 2 lists and create the resource

like image 756
Datta Avatar asked Jun 08 '26 13:06

Datta


1 Answers

Yes, you can use setproduct to iterate over both bucket_name and access_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}  
}

then

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]
}
like image 96
Marcin Avatar answered Jun 10 '26 10:06

Marcin