有没有办法部署不在主文件中的 Azure 资源模块?

Is there a way to deploy Azure resource modules not in the main file?

通常在通过 Bicep 模块创建 Azure 资源时,我会有 2 个文件。一个文件指定保存参数化资源和另一个文件,即主文件,它将使用该模块。

作为创建操作组的示例,我的资源文件如下所示:

动作-group.bicep

param actionGroupName string
param groupShortName string
param emailReceivers array = []

// Alerting Action Group
resource action_group 'microsoft.insights/actionGroups@2019-06-01' = {
  name: actionGroupName
  location: 'global'
  tags: {}
  properties: {
    groupShortName: groupShortName
    enabled: true
    emailReceivers: emailReceivers
  }
}

此资源随后在主文件中作为模块使用,main.bicep:

// Alerting Action Group
module actionGroup '../modules/alerts/alert-group.bicep' = {
  name: 'action-group-dply'
  params: {
    actionGroupName: actionGroupName
    groupShortName: actionGroupShortName
    emailReceivers: [
      {        
        name: '<Name of email receivers>'
        emailAddress: alertEmailList
      }
    ]
  }
}

我的管道引用 main.bicep 并将部署文件中列出的资源。我的问题是,有没有办法在混音中添加第三个文件?一个文件仍然保存参数化资源,一个文件保存关联的资源模块,以及 main.bicep 文件。我的想法是在我现有的资源中创建各种警报,但我不想向 main.bicep 添加大量模块,因为它会迅速增加此文件中的复杂性和代码量。

有没有办法让我拥有这个模块文件,并在 main.bicep 中引用整个文件以仍然从原始管道部署?

举个例子: alerts.bicep

param metricAlertsName string
param description string
param severity int
param enabled bool = true
param scopes array = []
param evaluationFrequency string
param windowSize string
param targetResourceRegion string = resourceGroup().location
param allOf array = []
param actionGroupName string

var actionGroupId = resourceId(resourceGroup().name, 'microsoft.insights/actionGroups', actionGroupName)

resource dealsMetricAlerts 'Microsoft.Insights/metricAlerts@2018-03-01' = {
  name: metricAlertsName
  location: 'global'
  tags: {}
  properties: {
    description: description
    severity: severity
    enabled: enabled
    scopes: scopes
    evaluationFrequency: evaluationFrequency
    windowSize: windowSize
    targetResourceRegion: targetResourceRegion
    criteria: {
      'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
      allOf: allOf
    }
    actions: [
      {
        actionGroupId: actionGroupId
      }
    ]
  }

警报-modules.bicep

// Function/Web Apps 403 Error
module appServicePlan403Errors '../modules/alerts/alerts.bicep' = {
   // Alert Logic
}

// Function/Web Apps 500 Error
module appServicePlan500Errors '../modules/alerts/alerts.bicep' = {
  // Alert Logic
}

main.bicep

// Some reference to alert-modules.bicep so when the pipeline runs and looks for main.bicep, it will still deploy all the resources

您可以使用循环多次调用该模块(在 main.bicep 或一个模块中):

https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/loops

例如

param alertsCollection array = [
  {
    actionGroupName: 'group1'
    groupShortName: 'g1'
    emailReceivers: []
  }
  {
    actionGroupName: 'group2'
    groupShortName: 'g2'
    emailReceivers: [
      'foo@bar.com'
      'bar@baz.com'
    ]
  }
]

module alerts '../modules/alerts/alert-group.bicep' = [for alert in alertsCollection): {
  name: '${alert.actionGroupName}'
  params: {
    actionGroupName: alert.actionGroupName
    groupShortName: alert.groupShortName
    emailReceivers: alert.emailReceivers
 }
}]

您可以简化通过以下方式传递的参数:

module alerts '../modules/alerts/alert-group.bicep' = [for alert in alertsCollection): {
  name: '${alert.actionGroupName}'
  params: alert
}]

但是您需要对 alertsCollection 参数的架构非常严格...

有帮助吗?