Skip to main content

Resource Alarm Discord

📋 Table of Contents

  1. Overview
  2. Function Structure
  3. Key Components
  4. Environment Variables
  5. Discord Webhook Payload
  6. Error Handling
  7. Best Practices
  8. 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:

  1. Main Handler: Processes incoming CloudWatch Alarm events.
  2. Alarm Color Determination: Sets the color of the Discord embed based on the alarm state.
  3. Discord Payload Creation: Formats alarm data for Discord.
  4. 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

  1. Secure Webhook URL: Always store the Discord webhook URL as an environment variable, never hardcode it.
  2. Informative Alerts: Include relevant details in the Discord message to facilitate quick understanding and response.
  3. Color Coding: Use colors effectively to distinguish between different alarm states at a glance.
  4. Error Logging: Log errors comprehensively to aid in troubleshooting.
  5. Rate Limiting: Be aware of Discord's rate limits and implement backoff strategies if necessary.
  6. Environment-Specific Alerts: Customize messages based on the environment to provide context-aware notifications.

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.