skip to Main Content

I am deploying very simple Azure log search alert rules using Bicep.
Code sample:

param alertLocation string = resourceGroup().location

var umAlertRules = [
  {
    name: 'Update Manager Schedule Failures'
    query: 'here is query, not important for explanation to have here'
    description: 'Update schedules with status other than succeded'
    displayName: 'Update Manager Schedule Failures'
    enabled: true
    evaluationFrequency: 'PT5M'
    severity: 1
    windowSize: 'PT5M'
  }
]

resource umAlertRule 'Microsoft.Insights/scheduledQueryRules@2023-03-15-preview' = [for umAlertRule in umAlertRules:{
  name: umAlertRule.name
  location: alertLocation
  tags: resourceGroup().tags
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '/subscriptions/subscription_id/resourceGroups/myRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/idName': {}
    }
  }
  properties: {
    actions: {
      actionGroups: [
        'subscriptions/subscription_id/resourceGroups/myRG/providers/microsoft.insights/actiongroups/ag-updatemanager'
      ]
      actionProperties: {}
      customProperties: {}
    }
    autoMitigate: false
    criteria: {
      allOf: [
        {
          dimensions: []
          failingPeriods: {
            minFailingPeriodsToAlert: 1
            numberOfEvaluationPeriods: 1
          }
          operator: (contains(umAlertRule, 'criteriaOperator')) ? umAlertRule.criteriaOperator : 'GreaterThanOrEqual' // THIS PART IS CAUSING ISSUE IN ADO PIPELINE
          query: umAlertRule.query
          threshold: 1
          timeAggregation: 'Count'
        }
      ]
    }
    description: umAlertRule.description
    displayName: umAlertRule.displayName
    enabled: umAlertRule.enabled
    evaluationFrequency: umAlertRule.evaluationFrequency
    muteActionsDuration: (contains(umAlertRule, 'muteActionsDuration')) ? umAlertRule.muteActionsDuration : null  // THIS PART IS CAUSING ISSUE IN ADO PIPELINE
    scopes: [
      'subscriptions/subscription_id'
    ]
    severity: umAlertRule.severity
    skipQueryValidation: true
    targetResourceTypes: []
    windowSize: umAlertRule.windowSize
  }
}]

When I am running Bicep configuration on localhost using command az deployment group what-if on WSL Ubuntu or using Powershell Core command New-AzResourceGroupDeployment, there is no error reported and deployment succeeds.
This is on output operator: "GreaterThanOrEqual"

But using following tasks in Azure DevOps:

- task: AzureCLI@2
  name: BicelPlan
  displayName: Bicep Plan
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: 'bash'
    workingDirectory: 'bicep'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az deployment group what-if 
      --resource-group 'myRG' 
      --template-file 'um_alerts.bicep'

pipeline using public runner, it fails with following error:

/home/vsts/work/1/s/bicep/um_alerts.bicep(75,79) : Error BCP053: The type "object" does not contain property "criteriaOperator". Available properties include "description", "displayName", "enabled", "evaluationFrequency", "name", "query", "severity", "windowSize".*

The goal of this, is to use similar approach as in Terraform locals, where not all properties are specified and try function is used in configuration.

Did anyone experienced same? Obviously the point here is to DRY.

2

Answers


  1. Your object here:

    var umAlertRules = [
      {
        name: 'Update Manager Schedule Failures'
        query: 'here is query, not important for explanation to have here'
        description: 'Update schedules with status other than succeded'
        displayName: 'Update Manager Schedule Failures'
        enabled: true
        evaluationFrequency: 'PT5M'
        severity: 1
        windowSize: 'PT5M'
      }
    ]
    

    does not contain criteriaOperator property. Add it with your default value:

    var umAlertRules = [
      {
        name: 'Update Manager Schedule Failures'
        query: 'here is query, not important for explanation to have here'
        description: 'Update schedules with status other than succeded'
        displayName: 'Update Manager Schedule Failures'
        enabled: true
        evaluationFrequency: 'PT5M'
        severity: 1
        windowSize: 'PT5M'
        criteriaOperator: 'GreaterThanOrEqual'
      }
    ]
    
    Login or Signup to reply.
  2. Your dry principle works, it’s just that your var is not a object, it’s an array of objects, with one object in it. Your simplest solution is to change your var:

    var umAlertRules = [
      {
        name: 'Update Manager Schedule Failures'
        query: 'here is query, not important for explanation to have here'
        description: 'Update schedules with status other than succeded'
        displayName: 'Update Manager Schedule Failures'
        enabled: true
        evaluationFrequency: 'PT5M'
        severity: 1
        windowSize: 'PT5M'
      }
    ]
    

    into

    var umAlertRules = {
        name: 'Update Manager Schedule Failures'
        query: 'here is query, not important for explanation to have here'
        description: 'Update schedules with status other than succeded'
        displayName: 'Update Manager Schedule Failures'
        enabled: true
        evaluationFrequency: 'PT5M'
        severity: 1
        windowSize: 'PT5M'
    }
    

    Now your ternary operator will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search