Home > other >  Azure bicep loop storage accounts
Azure bicep loop storage accounts

Time:09-28

I am still unfamiliar with azure bicep but I really like what Microsoft have done with it so far.

I am still learning the syntax and the basic of it but I got stuck at this point.

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 am trying to create this in a loop, so I can create multiple storages, with the same configuration.

So far I get to this point.

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, on the last line }] I am getting the following error:

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

I don't understand why I am getting this syntax error, all the }] seems to be correct.

And one think I don't understand is this. When I create manually the storage account, the policy configurations for Soft delete etc are made at storage account level. But following the documentations, this configuration is done at single blob storage.

Can please anyone give me a explanation about this and the best approach?

Thank you very much for any help you can provide

CodePudding user response:

Please try by changing the following line of code:

name: [storageName]

to

name: storageName

And you should not get the compilation error.

I tried the following in Bicep Playground:

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 got the following ARM template:

{
  "$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()])]"
      ]
    }
  ]
}
  • Related