Resource Alarm Discord
📋 Table of Contents
- Overview
- Function Structure
- Key Components
- Environment Variables
- Discord Webhook Payload
- Error Handling
- Best Practices
- Useful Links
🌟 Overview
This document outlines the Lambda function (resource-alarm-discord.js) responsible for processing CloudWatch Alarm events and sending notifications to Discord. This function is triggered when a CloudWatch Alarm changes state, providing real-time alerts for resource issues.
🏗️ Function Structure
The Lambda function is structured as follows:
- Main Handler: Processes incoming CloudWatch Alarm events.
- Alarm Color Determination: Sets the color of the Discord embed based on the alarm state.
- Discord Payload Creation: Formats alarm data for Discord.
- Discord Notification: Sends the formatted payload to a Discord webhook.
🔑 Key Components
Main Handler
The main handler function processes the incoming event:
exports.handler = async (event) => {
// ... (event processing logic)
};
Alarm Color Determination
This function sets the color of the Discord embed based on the alarm state:
const getAlarmColor = (newState) => {
return newState === "ALARM" ? 16711680 : 65280; // Red for ALARM, Green for OK
};
Discord Payload Creation
The function creates a structured payload for Discord, including relevant alarm information:
const embed = {
title: `CloudWatch Alarm: ${message.AlarmName}`,
color: getAlarmColor(message.NewStateValue),
fields: [
// ... (alarm details)
],
timestamp: new Date().toISOString()
};
const webhookBody = {
username: "CloudWatch Alarm Bot",
avatar_url: "https://cdn.discordapp.com/attachments/1314490272037605376/1322355890837520484/DALLE_2024-12-27_18.10.05_-_A_cute_but_slightly_menacing_ghost_holding_a_baseball_bat_with_an_expression_that_says_looking_for_trouble._The_ghost_is_small_and_round_with_soft.webp?ex=67709364&is=676f41e4&hm=0e65a3e0aea6610d6e9dd34eb7379addb77ddd7734d15262c2e475c69f82b577&",
content: "A CloudWatch Alarm has been triggered!",
embeds: [embed],
};
Discord Notification
This function sends the formatted payload to Discord:
const req = https.request(requestOptions, (res) => {
// ... (Discord API call logic)
});
🔧 Environment Variables
RESOURCE_ALARM_DISCORD_WEBHOOK_URL: The Discord webhook URL for sending notifications.APP_ENV: The application environment (e.g., "production", "staging").
📤 Discord Webhook Payload
The Discord webhook payload includes:
- A custom username and avatar for the bot
- A brief content message
- An embed with detailed alarm information, including:
- Alarm name
- Current state
- Environment
- Reason for the alarm
- Technical information
- AWS Region
- Metric details
The embed's color is dynamic: red for ALARM state and green for OK state.
⚠️ Error Handling
The function includes error handling for various scenarios:
- Missing webhook URL
- JSON parsing errors
- HTTP request errors
Errors are logged to CloudWatch Logs for troubleshooting.
📝 Best Practices
- Secure Webhook URL: Always store the Discord webhook URL as an environment variable, never hardcode it.
- Informative Alerts: Include relevant details in the Discord message to facilitate quick understanding and response.
- Color Coding: Use colors effectively to distinguish between different alarm states at a glance.
- Error Logging: Log errors comprehensively to aid in troubleshooting.
- Rate Limiting: Be aware of Discord's rate limits and implement backoff strategies if necessary.
- Environment-Specific Alerts: Customize messages based on the environment to provide context-aware notifications.
🔗 Useful Links
Remember to keep this document up-to-date as you modify the Lambda function. Regular reviews will ensure that your alarm notification process remains effective and aligned with your operational needs.