Azure bicep 循环存储帐户

Azure bicep loop storage accounts

我对 azure bicep 仍然不熟悉,但我真的很喜欢微软到目前为止用它所做的一切。

我仍在学习语法及其基础知识,但我卡在了这一点上。

我想做的是复制存储帐户的创建并设置一些配置,例如 minimum_tls_version DeleteRetentionPolicy

我试图在循环中创建它,因此我可以创建多个具有相同配置的存储。

到此为止。

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
  }
}]

此时,在最后一行 }] 我收到以下错误:

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

我不明白为什么会出现这个语法错误,所有 }] 似乎都是正确的。

我想我不明白的是这个。当我手动创建存储帐户时,Soft delete etc 的策略配置是在存储帐户级别进行的。但是按照文档,这个配置是在单个 blob storage.

完成的

谁能给我一个解释和最好的方法?

非常感谢您提供的任何帮助

请尝试更改以下代码行:

name: [storageName]

name: storageName

而且你应该不会得到编译错误。

我在 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
  }
}]

并获得了以下 ARM 模板:

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