How to add Diagnostic Settings to Azure Automation via Bicep

Configuring Diagnostic Settings via Bicep (or ARM templates) is not obvious. There are 4 major steps.

  1. Create Log Analytics workspace
  2. Create Azure Automation account
  3. Create link between Log Analytics Azure Automation
  4. Add Diagnostic Settings

Create Log Analytics workspace

resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-08-01' = {
  name: logAnalyticsWorkspaceName
  location: location
  properties: {
    sku: {
      name: 'Standard'
    }
  }
}

Create Azure Automation account

resource automationAccount 'Microsoft.Automation/automationAccounts@2021-06-22' = {
  name: automationAccountName
  location: location
  properties: {
    sku: {
      name: 'Basic'
    }
  }
}

Create link between Log Analytics Azure Automation

This is the part that is a little different from other Azure resources. You need to create a linkedService before setting up the diagnostic settings. You will need this “name” later, so store it in a variable.

var automationAccountLinkedWorkspaceName = 'Automation'

resource automationAccountLinkedWorkspace 'Microsoft.OperationalInsights/workspaces/linkedServices@2020-08-01' = {
  name: '${logAnalyticsWorkspace.name}/${automationAccountLinkedWorkspaceName}'
  location: location
  properties: {
    resourceId: '${automationAccount.id}'
  }
}

Add Diagnostic Settings

resource diagnosticSettings 'Microsoft.Automation/automationAccounts/providers/diagnosticSettings@2021-05-01-preview' = {
  name: '${automationAccount.name}/Microsoft.Insights/${automationAccountLinkedWorkspaceName}'
  location: location
  properties: {
    name: automationAccountLinkedWorkspaceName
    workspaceId: logAnalyticsWorkspace.id
    logs: [
      {
        category: 'JobLogs'
        enabled: true
        retentionPolicy: {
          enabled: true
          days: 0
        }
      }
      {
        category: 'JobStreams'
        enabled: true
        retentionPolicy: {
          enabled: true
          days: 0
        }
      }
      {
        category: 'DscNodeStatus'
        enabled: true
        retentionPolicy: {
          enabled: true
          days: 0
        }
      }
      {
        category: 'AuditEvent'
        enabled: true
        retentionPolicy: {
          enabled: true
          days: 0
        }
      }
    ]
    metrics: [
      {
        category: 'AllMetrics'
        enabled: true
        retentionPolicy: {
          enabled: true
          days: 0
        }
      }
    ]
  }
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *