Skip to content

Commit

Permalink
Configure Logic App to send Azure Alerts to Azure (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasanker authored Apr 14, 2024
1 parent 6f97170 commit 52b6913
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 65 deletions.
20 changes: 19 additions & 1 deletion azuredeploy.bicep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
targetScope = 'subscription'

@allowed([ 'dev', 'prd' ])
@allowed(['dev', 'prd'])
param environment string

var location = 'West Europe'
Expand Down Expand Up @@ -47,3 +47,21 @@ module alertLogicApp 'modules/alert-logicapp.bicep' = {
environment: environment
}
}

module actionGroup 'modules/actiongroup.bicep' = {
name: '${deployment().name}-actiongroup'
scope: sharedRg
params: {
organizationPrefix: organizationPrefix
sharedResourcesAbbreviation: sharedResourcesAbbreviation
environment: environment
emailReceivers: ['[email protected]']
logicAppReceivers: [
{
name: alertLogicApp.outputs.logicAppName
resourceId: alertLogicApp.outputs.logicAppResourceId
useCommonAlertSchema: true
}
]
}
}
33 changes: 33 additions & 0 deletions modules/actiongroup.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@allowed(['dev', 'prd'])
param environment string

param organizationPrefix string
param sharedResourcesAbbreviation string

param groupShortName string = 'IO Devs'
param emailReceivers array
param logicAppReceivers array

resource actiongroup 'Microsoft.Insights/actionGroups@2023-01-01' = {
name: 'ag-${organizationPrefix}-${sharedResourcesAbbreviation}-${environment}'
location: 'Global'
properties: {
groupShortName: groupShortName
enabled: true
emailReceivers: [
for email in emailReceivers: {
name: email
emailAddress: email
useCommonAlertSchema: true
}
]
logicAppReceivers: [
for app in logicAppReceivers: {
name: app.name
resourceId: app.ResourceId
callbackUrl: listCallbackUrl(app.ResourceId, '2019-05-01').value
useCommonAlertSchema: app.UseCommonAlertSchema
}
]
}
}
149 changes: 85 additions & 64 deletions modules/alert-logicapp.bicep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@allowed([ 'dev', 'prd' ])
@allowed(['dev', 'prd'])
param environment string
param location string = resourceGroup().location

Expand All @@ -7,17 +7,18 @@ param sharedResourcesAbbreviation string

var slackConnectionName = 'AnalogIO'

@description('The Slack channel to post to.')
@description('The Slack channel to post to')
param slackChannel string = 'io-alerts'

var connectionId = subscriptionResourceId('Microsoft.Web/locations/managedApis', location, 'slack')
resource slackConnection 'Microsoft.Web/connections@2016-06-01' = {
location: location
name: slackConnectionName
properties: {
api: {
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, 'slack')
id: connectionId
}
displayName: 'slack'
displayName: 'CafeAnalog'
}
}

Expand All @@ -27,100 +28,120 @@ resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
properties: {
definition: {
'$schema': 'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'
actions: {
'Post_message_(V2)': {
inputs: {
body: {
channel: slackChannel
text: ':rotating_light: :rotating_light: *Azure alert `@{triggerBody()?[\'data\']?[\'essentials\']?[\'alertRule\']}` (severity @{triggerBody()?[\'data\']?[\'essentials\']?[\'severity\']}) is @{triggerBody()?[\'data\']?[\'essentials\']?[\'monitorCondition\']}*\n\nAffected Resources\n```\n@{join(triggerBody()?[\'data\']?[\'essentials\']?[\'alertTargetIDs\'], \'\n\')}\n```\n'
}
host: {
connection: {
name: '@parameters(\'$connections\')[\'slack\'][\'connectionId\']'
}
}
method: 'post'
path: '/v2/chat.postMessage'
}
runAfter: {}
type: 'ApiConnection'
}
}
contentVersion: '1.0.0.0'
outputs: {}
parameters: {
'$connections': {
defaultValue: {}
type: 'Object'
}
}
triggers: {
manual: {
type: 'request'
kind: 'Http'
HTTP_Request: {
inputs: {
schema: {
'$schema': 'http://json-schema.org/draft-04/schema#'
properties: {
context: {
data: {
properties: {
name: {
type: 'string'
}
portalLink: {
type: 'string'
alertContext: {
properties: {}
type: 'object'
}
resourceName: {
type: 'string'
essentials: {
properties: {
alertContextVersion: {
type: 'string'
}
alertId: {
type: 'string'
}
alertRule: {
type: 'string'
}
alertTargetIDs: {
items: {
type: 'string'
}
type: 'array'
}
description: {
type: 'string'
}
essentialsVersion: {
type: 'string'
}
firedDateTime: {
type: 'string'
}
monitorCondition: {
type: 'string'
}
monitoringService: {
type: 'string'
}
originAlertId: {
type: 'string'
}
resolvedDateTime: {
type: 'string'
}
severity: {
type: 'string'
}
signalType: {
type: 'string'
}
}
type: 'object'
}
}
required: [
'name'
'portalLink'
'resourceName'
]
type: 'object'
}
status: {
schemaId: {
type: 'string'
}
}
required: [
'status'
'context'
]
type: 'object'
}
}
kind: 'Http'
operationOptions: 'EnableSchemaValidation'
type: 'Request'
}
}
actions: {
Http: {
type: 'Http'
inputs: {
body: {
longUrl: '@{triggerBody()[\'context\'][\'portalLink\']}'
}
headers: {
'Content-Type': 'application/json'
}
method: 'POST'
uri: 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBkT1BRbA-uULHz8HMUAi0ywJtpNLXHShI'
}
}
Post_Message: {
type: 'ApiConnection'
inputs: {
host: {
connection: {
name: '@parameters(\'$connections\')[\'slack\'][\'connectionId\']'
}
}
method: 'post'
path: '/chat.postMessage'
queries: {
channel: slackChannel
text: 'Azure Alert - \'@{triggerBody()[\'context\'][\'name\']}\' @{triggerBody()[\'status\']} on \'@{triggerBody()[\'context\'][\'resourceName\']}\'. Details: @{body(\'Http\')[\'id\']}'
}
}
runAfter: {
Http: [
'Succeeded'
]
}
}
}
outputs: {}
}
parameters: {
'$connections': {
value: {
slack: {
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, 'slack')
connectionId: slackConnection.id
connectionName: slackConnection.name
id: connectionId
}
}
}
}
}
}

output logicAppName string = logicApp.name
output logicAppResourceId string = logicApp.id

0 comments on commit 52b6913

Please sign in to comment.