Home > Software design >  Bicep Template ~ Create directories in ADLS
Bicep Template ~ Create directories in ADLS

Time:12-30

I'm trying to transition from Terraform with the azurerm provider to Bicep and having a tohgh time figuring out how to use Bicep to populate my required ADLS Gen 2 directory structure.

Many of my builds need a new ADLS with lots of containers, then inside each container I need nested directories, sometimes three or four levels deep. In Terraform this is pretty simple using the storage/path resource type: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_data_lake_gen2_path

I cannot see any equivalent using Bicep. What am I missing? The only possibility that seems remotely feasible to accomplish this is to package a deployment Powershell or CLI script, but that looks like a lot of extra configuration and effort for something this trivial.

I would provide code samples for my bicep, but they would only show the storage and container resources and probably would not be very useful. The container resource works perfectly for creating the root directories - but I cannot see how to get Bicep to create directories/paths within them. Can anyone assist, please?

Update

Does not appear to be possible in Bicep. Happy to be proven wrong.

CodePudding user response:

You will have to work with a deployment script to create directories in ADLS Gen2 file systems. You can include inline Azure CLI scripts in Bicep templates. You can create the directory using the Azure CLI command az storage fs directory create. If we include this command in a Bicep template deploying a ADLS Gen 2 storage account, it would look like this:

param location string = 'westeurope'
param containerName string = 'mycontainer'
param directoryName string = 'mydirectory'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: uniqueString(resourceGroup().id)
  kind: 'StorageV2'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  properties: {
    isHnsEnabled: true
  }

  resource container 'blobServices@2022-09-01' = {
    name: 'default'

    resource container 'containers@2022-09-01' = {
      name: containerName
    }
  }
}

resource createDirectory 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: 'createDirectory'
  kind: 'AzureCLI'
  location: location
  properties:{
    azCliVersion: '2.42.0'
    retentionInterval: 'P1D'
    arguments: '\'${storageAccount.name}\' \'${containerName}\' \'${directoryName}\''
    scriptContent: 'az storage fs directory create --account-name $1 -f $2 -n $3 --auth-mode key'
    environmentVariables: [
      {
        name: 'AZURE_STORAGE_KEY'
        secureValue: storageAccount.listKeys().keys[0].value
      }
    ]
  }
}

  • Related