Home > database >  how to use Loops in Terraform to deploy 2 different azure resources
how to use Loops in Terraform to deploy 2 different azure resources

Time:04-09

I'm currently writing some Terraform code that deploys Azure Datalake Gen2 Storage Filesystem and folder structure for each filesystem.

Below code works fine when creating all the filesystems declared in the variable list.

variable.tf

variable "product_unified_filesystem" {
   default =  [
     "product1",
     "product2",
     "product3"
     ]
}

variable "product_unified_subdirs" {
  default = [
    "subdirectory1",
    "subdirectory2",
    "subdirectory3"
  ]
}

main.tf

resource "azurerm_storage_data_lake_gen2_filesystem" "unified-filesystem" {
  count              = length(var.product_unified_filesystem)
  name               = var.product_unified_filesystem[count.index] 
  storage_account_id = module.storage.id
}

resource "azurerm_storage_data_lake_gen2_path" "unified-subdirs-1" {
  count              = length(var.product_unified_subdirs)
  path               = var.product_unified_subdirs[count.index]  
  filesystem_name    = "product1"
  storage_account_id = module.storage.id
  resource           = "directory"
  }
}
resource "azurerm_storage_data_lake_gen2_path" "unified-subdirs-2" {
  count              = length(var.product_unified_subdirs)
  path               = var.product_unified_subdirs[count.index]  
  filesystem_name    = "product2"
  storage_account_id = module.storage.id
  resource           = "directory"
  }
}

resource "azurerm_storage_data_lake_gen2_path" "unified-subdirs-3" {
  count              = length(var.product_unified_subdirs)
  path               = var.product_unified_subdirs[count.index]  
  filesystem_name    = "product3"
  storage_account_id = module.storage.id
  resource           = "directory"
  }
}

Now I want to create a common sub folder structure each of the product file system created above.

Above Code works when I pass one filesystem at a time to create the folder structure.

I want to loop through both variables and create a folder structure as shown below.

  • product1 subdirectory1 subdirectory2 subdirectory3
  • product2 subdirectory1 subdirectory2 subdirectory3
  • product3 subdirectory1 subdirectory2 subdirectory3

CodePudding user response:

You can use setproduct to get all combinations. Also its better to use for_each instead of count as it does not depend on ordering of items.

locals {
  fs_subdir = {
    for val in setproduct(var.product_unified_filesystem, var.product_unified_subdirs):
      "${val[0]}-${val[1]}" => {
        product = val[0]
        subdir = val[1]
      }
  }
}

then


resource "azurerm_storage_data_lake_gen2_filesystem" "unified-filesystem" {
  for_each           = toset(var.product_unified_filesystem)
  name               = each.key
  storage_account_id = module.storage.id
}

resource "azurerm_storage_data_lake_gen2_path" "unified-subdirs" {
  for_each           = local.fs_subdir
  path               = each.value.subdir
  filesystem_name    = each.value.product
  storage_account_id = module.storage.id
  resource           = "directory"
  }
}
  • Related