Coder Perfect

Is it possible to publish Azure resource modules that aren’t included in the main file?

Problem

Normally, I would have two files when building Azure resources using Bicep modules. One file for the parameterized resource and another, the main file, for the module’s consumption.

My resource file for building an action group looks like this:

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

After that, this resource is used as a module in the main file, 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
      }
    ]
  }
}

My pipeline uses main.bicep to deploy the resources provided in the file. I’m wondering whether there’s a way to add a third file to the mix. There are three files: one for the parameterized resource, one for the associated resource modules, and one for the main.bicep file. The aim is to establish numerous alerts throughout my existing resources, but I don’t want to add too many modules to main.bicep because it will quickly grow the file’s complexity and code.

Is there a way to have this file of modules and still deploy from the original pipeline by referencing the full file in main.bicep?

For instance, 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
      }
    ]
  }

alert-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

Asked by agw2021

Solution #1

Using looping, you can call the module many times (in main.bicep or a module):

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

e.g.

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

You may make the params passing easier by using:

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

However, the alertsCollection parameter’s schema must be strictly adhered to…

That help?

Answered by bmoore-msft

Post is based on https://stackoverflow.com/questions/69742629/is-there-a-way-to-deploy-azure-resource-modules-not-in-the-main-file