🚨 Send AWS Health Notifications to Slack Using Lambda and EventBridge
Getting alerts when something is wrong with your AWS services is important—especially if you’re running production systems. In this guide, you’ll learn how to automatically send AWS Health notifications to Slack using AWS Lambda and EventBridge.
đź”— Step 1: Create a Slack Webhook
-
Go to your Slack channel.
-
Search for "Incoming Webhooks" in the Slack App Directory.
-
Add it and choose the channel to post messages to.
-
Copy the Webhook URL (you’ll need this for Lambda).
🖥️ Step 2: Create the Lambda Function
Go to the AWS Lambda console and create a new function. Use Python and paste the following code:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
from urllib.request import Request, urlopen, URLError, HTTPError | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
# Replace this with your Slack webhook URL | |
slack_webhook_url = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" | |
def lambda_handler(event, context): | |
description = event['detail']['eventDescription'][0]['latestDescription'] | |
eventArn = event['detail']['eventArn'] | |
dashboard_url = f"https://phd.aws.amazon.com/phd/home?region=us-east-1#/event-log?eventID={eventArn}" | |
message = { | |
"attachments": [ | |
{ | |
"color": "#E01E5A", | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": {"type": "plain_text", "text": "🚨 AWS Health Alert", "emoji": True} | |
}, | |
{ | |
"type": "section", | |
"text": {"type": "mrkdwn", "text": f"*Description:*\n{description}"} | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{"type": "mrkdwn", "text": f"đź”— <{dashboard_url}|View in AWS Health Dashboard>"} | |
] | |
} | |
] | |
} | |
] | |
} | |
req = Request(slack_webhook_url, data=json.dumps(message).encode("utf-8"), headers={"Content-Type": "application/json"}) | |
try: | |
urlopen(req) | |
logger.info("Notification sent to Slack") | |
except HTTPError as e: | |
logger.error("HTTPError: %d %s", e.code, e.reason) | |
except URLError as e: | |
logger.error("URLError: %s", e.reason) |
🎯 Step 3: Create an EventBridge Rule
-
Go to Amazon EventBridge.
-
Create a new rule.
-
Choose Event Source: AWS services.
-
Pick AWS Health.
-
For event pattern, use:
OutPut:
Comments
Post a Comment