Coder Perfect

Accounts for Azure bicep loop storage

Problem

Although I am still inexperienced with Azure Bicep, I am impressed with what Microsoft has accomplished thus far.

I’m still learning the syntax and the fundamentals, but I’ve reached a snag.

What I am trying to do, is replicate the creation of a storage account and set some configuration such as the minimum_tls_version DeleteRetentionPolicy etc.

I’m trying to do this in a loop so that I can make several storages with the same settings.

So far, I’ve gotten this far.

param storageAccounts array = [
  'storage2'
]

resource storage_Accounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [ for storageName in storageAccounts :{
  name: [storageName]
  location: 'westeurope'
  sku: {
    name: 'Standard_RAGRS'
    tier: 'Standard'
  }
  kind: 'StorageV2'
  properties: {
    allowCrossTenantReplication: true
    minimumTlsVersion: 'TLS1_2'
    allowBlobPublicAccess: false
    allowSharedKeyAccess: true
    networkAcls: {
      bypass: 'AzureServices'
      virtualNetworkRules: []
      ipRules: []
      defaultAction: 'Allow'
    }
    supportsHttpsTrafficOnly: true
    encryption: {
      services: {
        file: {
          keyType: 'Account'
          enabled: true
        }
        blob: {
          keyType: 'Account'
          enabled: true
        }
      }
      keySource: 'Microsoft.Storage'
    }
    accessTier: 'Hot'
  }
}]

resource storage_Accounts_name_default 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [ for storageName in storageAccounts :{
  parent: storage_Accounts
  name: [storageName]
  properties: {
    changeFeed: {
      enabled: false
    }
    restorePolicy: {
      enabled: false
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    cors: {
      corsRules: []
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 30
    }
    isVersioningEnabled: true
  }
}]

At this point, I’m getting the following problem on the last line:

Expected the "}" character at this location.bicep(BCP018)
Expected the "]" character at this location.bicep(BCP018)

I’m not sure why I’m receiving this syntax issue because all of the ] appear to be correct.

And there’s one thing I’m not sure about. The policy parameters for Soft deletion, etc. are done at the storage account level when I create the storage account manually. But following the documentations, this configuration is done at single blob storage.

Could someone help explain this to me and suggest the best course of action?

Thank you so much for any assistance you may offer.

Asked by Nayden Van

Solution #1

Change the following line of code to see if it helps:

name: [storageName]

to

name: storageName

Also, the compilation error should not occur.

In Bicep Playground, I attempted the following:

param storageAccounts array = [
  'storage2'
]
resource storage_Accounts 'Microsoft.Storage/storageAccounts@2021-04-01' = [ for storageName in storageAccounts :{
  name: storageName
  location: 'westeurope'
  sku: {
    name: 'Standard_RAGRS'
    tier: 'Standard'
  }
  kind: 'StorageV2'
  properties: {
    allowCrossTenantReplication: true
    minimumTlsVersion: 'TLS1_2'
    allowBlobPublicAccess: false
    allowSharedKeyAccess: true
    networkAcls: {
      bypass: 'AzureServices'
      virtualNetworkRules: []
      ipRules: []
      defaultAction: 'Allow'
    }
    supportsHttpsTrafficOnly: true
    encryption: {
      services: {
        file: {
          keyType: 'Account'
          enabled: true
        }
        blob: {
          keyType: 'Account'
          enabled: true
        }
      }
      keySource: 'Microsoft.Storage'
    }
    accessTier: 'Hot'
  }
}]

resource storage_Accounts_name_default 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [ for (storageName, i) in storageAccounts :{
  parent: storage_Accounts[i]
  name: storageName
  properties: {
    changeFeed: {
      enabled: false
    }
    restorePolicy: {
      enabled: false
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    cors: {
      corsRules: []
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 30
    }
    isVersioningEnabled: true
  }
}]

and received the ARM template below:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.4.1.14562",
      "templateHash": "6966745077860153629"
    }
  },
  "parameters": {
    "storageAccounts": {
      "type": "array",
      "defaultValue": [
        "storage2"
      ]
    }
  },
  "functions": [],
  "resources": [
    {
      "copy": {
        "name": "storage_Accounts",
        "count": "[length(parameters('storageAccounts'))]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-04-01",
      "name": "[parameters('storageAccounts')[copyIndex()]]",
      "location": "westeurope",
      "sku": {
        "name": "Standard_RAGRS",
        "tier": "Standard"
      },
      "kind": "StorageV2",
      "properties": {
        "allowCrossTenantReplication": true,
        "minimumTlsVersion": "TLS1_2",
        "allowBlobPublicAccess": false,
        "allowSharedKeyAccess": true,
        "networkAcls": {
          "bypass": "AzureServices",
          "virtualNetworkRules": [],
          "ipRules": [],
          "defaultAction": "Allow"
        },
        "supportsHttpsTrafficOnly": true,
        "encryption": {
          "services": {
            "file": {
              "keyType": "Account",
              "enabled": true
            },
            "blob": {
              "keyType": "Account",
              "enabled": true
            }
          },
          "keySource": "Microsoft.Storage"
        },
        "accessTier": "Hot"
      }
    },
    {
      "copy": {
        "name": "storage_Accounts_name_default",
        "count": "[length(parameters('storageAccounts'))]"
      },
      "type": "Microsoft.Storage/storageAccounts/blobServices",
      "apiVersion": "2021-04-01",
      "name": "[format('{0}/{1}', parameters('storageAccounts')[copyIndex()], parameters('storageAccounts')[copyIndex()])]",
      "properties": {
        "changeFeed": {
          "enabled": false
        },
        "restorePolicy": {
          "enabled": false
        },
        "containerDeleteRetentionPolicy": {
          "enabled": true,
          "days": 7
        },
        "cors": {
          "corsRules": []
        },
        "deleteRetentionPolicy": {
          "enabled": true,
          "days": 30
        },
        "isVersioningEnabled": true
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccounts')[copyIndex()])]"
      ]
    }
  ]
}

Answered by Gaurav Mantri

Post is based on https://stackoverflow.com/questions/69346453/azure-bicep-loop-storage-accounts