|
| 1 | +resource "azurerm_logic_app_workflow" "this" { |
| 2 | + name = var.name |
| 3 | + resource_group_name = var.resource_group_name |
| 4 | + location = var.location |
| 5 | + |
| 6 | + workflow_parameters = { |
| 7 | + "slackWebhookUrl" = jsonencode({ |
| 8 | + type = "SecureString" |
| 9 | + defaultValue = "" |
| 10 | + }) |
| 11 | + } |
| 12 | + |
| 13 | + parameters = { |
| 14 | + "slackWebhookUrl" = var.slack_webhook_url |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +resource "azurerm_logic_app_trigger_http_request" "this" { |
| 19 | + name = "When_an_HTTP_request_is_received" |
| 20 | + logic_app_id = azurerm_logic_app_workflow.this.id |
| 21 | + method = "POST" |
| 22 | + |
| 23 | + schema = jsonencode({ |
| 24 | + "$schema" = "http://json-schema.org/draft-04/schema#" |
| 25 | + type = "object" |
| 26 | + properties = { |
| 27 | + schemaId = { type = "string" } |
| 28 | + data = { |
| 29 | + type = "object" |
| 30 | + properties = { |
| 31 | + essentials = { |
| 32 | + type = "object" |
| 33 | + properties = { |
| 34 | + alertRule = { type = "string" } |
| 35 | + severity = { type = "string" } |
| 36 | + firedDateTime = { type = "string" } |
| 37 | + resolvedDateTime = { type = "string" } |
| 38 | + monitorCondition = { type = "string" } |
| 39 | + description = { type = "string" } |
| 40 | + alertTargetIDs = { |
| 41 | + type = "array" |
| 42 | + items = { type = "string" } |
| 43 | + } |
| 44 | + configurationItems = { |
| 45 | + type = "array" |
| 46 | + items = { type = "string" } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +resource "azurerm_logic_app_action_custom" "post_to_slack" { |
| 57 | + name = "Post_to_Slack" |
| 58 | + logic_app_id = azurerm_logic_app_workflow.this.id |
| 59 | + |
| 60 | + body = <<-BODY |
| 61 | + { |
| 62 | + "type": "Http", |
| 63 | + "inputs": { |
| 64 | + "method": "POST", |
| 65 | + "uri": "@parameters('slackWebhookUrl')", |
| 66 | + "headers": { |
| 67 | + "Content-Type": "application/json" |
| 68 | + }, |
| 69 | + "body": { |
| 70 | + "blocks": [ |
| 71 | + { |
| 72 | + "type": "header", |
| 73 | + "text": { |
| 74 | + "type": "plain_text", |
| 75 | + "text": "@{if(equals(triggerBody()?['data']?['essentials']?['monitorCondition'], 'Resolved'), concat('✅ Resolved – ', triggerBody()?['data']?['essentials']?['alertRule']), concat('🚨 Alert – ', triggerBody()?['data']?['essentials']?['alertRule']))}", |
| 76 | + "emoji": true |
| 77 | + } |
| 78 | + }, |
| 79 | + { |
| 80 | + "type": "section", |
| 81 | + "fields": [ |
| 82 | + { |
| 83 | + "type": "mrkdwn", |
| 84 | + "text": "@{concat('*Severity*\n', triggerBody()?['data']?['essentials']?['severity'])}" |
| 85 | + }, |
| 86 | + { |
| 87 | + "type": "mrkdwn", |
| 88 | + "text": "@{concat('*Status*\n', triggerBody()?['data']?['essentials']?['monitorCondition'])}" |
| 89 | + }, |
| 90 | + { |
| 91 | + "type": "mrkdwn", |
| 92 | + "text": "@{if(equals(triggerBody()?['data']?['essentials']?['monitorCondition'], 'Resolved'), concat('*Resolved At*\n', triggerBody()?['data']?['essentials']?['resolvedDateTime']), concat('*Fired At*\n', triggerBody()?['data']?['essentials']?['firedDateTime']))}" |
| 93 | + }, |
| 94 | + { |
| 95 | + "type": "mrkdwn", |
| 96 | + "text": "@{concat('*Resource*\n`', coalesce(triggerBody()?['data']?['essentials']?['configurationItems']?[0], 'N/A'), '`')}" |
| 97 | + } |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "type": "section", |
| 102 | + "text": { |
| 103 | + "type": "mrkdwn", |
| 104 | + "text": "@{concat('*Description*\n> ', coalesce(triggerBody()?['data']?['essentials']?['description'], 'N/A'))}" |
| 105 | + } |
| 106 | + }, |
| 107 | + { |
| 108 | + "type": "section", |
| 109 | + "text": { |
| 110 | + "type": "mrkdwn", |
| 111 | + "text": "@{if(contains(coalesce(triggerBody()?['data']?['essentials']?['alertTargetIDs']?[0], ''), 'microsoft.insights/components'), concat(':mag: <https://portal.azure.com/#resource', triggerBody()?['data']?['essentials']?['alertTargetIDs']?[0], '/failures|View Failures in App Insights>'), concat(':mag: <https://portal.azure.com/#resource', coalesce(triggerBody()?['data']?['essentials']?['alertTargetIDs']?[0], ''), '|View Resource in Azure Portal>'))}" |
| 112 | + } |
| 113 | + } |
| 114 | + ] |
| 115 | + } |
| 116 | + }, |
| 117 | + "runAfter": {} |
| 118 | + } |
| 119 | + BODY |
| 120 | + |
| 121 | + depends_on = [azurerm_logic_app_trigger_http_request.this] |
| 122 | +} |
0 commit comments