0

我正在寻找可以创建多个存储帐户并使用 Bicep 中的循环为每个存储帐户创建诸如 blob 之类的子资源的解决方案。我能够部署多个存储帐户,但没有获得有关如何为迭代创建子资源的良好示例或文档肱二头肌资源

@allowed([
  'CACN'
  'CAEA'
  'USE2'
  'USCN'
])
param regionCode string
param mandatoryTags object
param sku object = {
  name: 'Standard_LRS'
  tier: 'Standard'
}
param identity object = {
  type: 'SystemAssigned'
}
@allowed([
  'None'
  'Logging'
  'Metrics'
  'AzureServices'
])
param bypass string = 'AzureServices'

// array of storage account names
param storageAccounts array

// variables 
var kind = 'StorageV2'
var varHTTPSOnly = true 
var varEnableNFSv3 = false
var varAllowBlobPublicAccess = false
var tlsVersion = 'TLS1_2'
var enableADLSgen2 = false

resource storageAccountResources 'Microsoft.Storage/storageAccounts@2021-02-01' = [for storageName in storageAccounts: {
  name: storageName
  location: resourceGroup().location
  kind: kind
  sku: sku
  tags: mandatoryTags
  identity: identity 
  properties: {
    accessTier: 'Hot'
    supportsHttpsTrafficOnly: varHTTPSOnly
    isHnsEnabled: enableADLSgen2
    minimumTlsVersion: tlsVersion
    isNfsV3Enabled: varEnableNFSv3
    allowBlobPublicAccess: varAllowBlobPublicAccess
    // network security controls
    networkAcls: {
      bypass: bypass     
      defaultAction: 'Deny'
      resourceAccessRules: []
      virtualNetworkRules: []
      ipRules: []
    } 
    routingPreference: {
      routingChoice: 'MicrosoftRouting'
      publishMicrosoftEndpoints: false
      publishInternetEndpoints: false
    }
    encryption: {
      services: {
        blob: {
          enabled: true
          keyType: 'Account'
        }
        file: {
          enabled: true
          keyType: 'Account'
        }
        table: {
          enabled: true
          keyType: 'Account'
        }
        queue: {
          enabled: true
          keyType: 'Account'
        }
      }
      keySource: 'Microsoft.Storage'
  }
  }
}]
output stgOutput array = [for (name, i) in storageAccounts: {
  name: storageAccounts[i].name
}]

resource ${storageName}_default 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = {
    name: 'default'
    properties: {
      changeFeed: {
        enabled: false
      }
      restorePolicy : {
        enabled: false
      }
      containerDeleteRetentionPolicy: {
        enabled: true
        days: 7
      }
      cors: {
        corsRules: []
      }
      deleteRetentionPolicy: {
        enabled: true
        days: 7        
      }
      isVersioningEnabled: true
    }
  } 
4

1 回答 1

0

查看文档(子资源的迭代),您应该能够使用迭代器:

resource blobServices 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [for i in range(0, length(storageAccounts)): {
  name: '${storageAccountResources[i].name}/default'
  properties: {
    changeFeed: {
      enabled: false
    }
    restorePolicy: {
      enabled: false
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    cors: {
      corsRules: []
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    isVersioningEnabled: true
  }
}]

对于复杂数组,您也可以使用以下语法:

resource blobServices 'Microsoft.Storage/storageAccounts/blobServices@2021-04-01' = [for (storageAccount, i) in storageAccounts: {
  name: '${storageAccountResources[i].name}/default'
  properties: {
    changeFeed: {
      enabled: storageAccount.changeFeed
    }
    restorePolicy: {
      enabled: storageAccount.restorePolicy
    }
    ...
  }
}]

于 2021-08-06T21:25:41.950 回答