Skip to main content

Error Filter Discord

📋 Table of Contents

  1. Overview
  2. Function Structure
  3. Key Components
  4. Environment Variables
  5. Error Handling
  6. Best Practices
  7. Useful Links

🌟 Overview

This document outlines the Lambda function (metric-filter-discord.js) responsible for processing CloudWatch Logs events and sending notifications to Discord. This function is triggered by the CloudWatch Logs subscription filter when error logs are detected.

🏗️ Function Structure

The Lambda function is structured as follows:

  1. Main Handler: Processes incoming CloudWatch Logs events.
  2. Decoding and Decompression: Extracts log data from the CloudWatch event.
  3. Discord Payload Creation: Formats log messages 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)
};

Decoding and Decompression

CloudWatch Logs data is base64 encoded and compressed. This function decodes and decompresses it:

async function decodeAndDecompress(base64Data) {
// ... (decoding and decompression logic)
}

Discord Payload Creation

This function formats the log messages for Discord:

function createDiscordPayloadForMessages(logsData, environment) {
// ... (payload creation logic)
}

Discord Notification

This function sends the formatted payload to Discord:

function sendToDiscord(webhookUrl, payload) {
// ... (Discord API call logic)
}

🔧 Environment Variables

  • ERROR_FILTER_DISCORD_WEBHOOK_URL: The Discord webhook URL for sending notifications.
  • APP_ENV: The application environment (e.g., "production", "staging").

⚠️ Error Handling

The function includes error handling for various scenarios:

  • Missing webhook URL
  • Unsupported event structures
  • Decompression and parsing errors
  • Discord API errors

📝 Best Practices

  1. Secure Webhook URL: Always store the Discord webhook URL as an environment variable, never hardcode it.
  2. Limit Message Size: Discord has a character limit for messages. Ensure your payloads don't exceed this limit.
  3. Error Logging: Log errors comprehensively to aid in troubleshooting.
  4. Rate Limiting: Be aware of Discord's rate limits and implement backoff strategies if necessary.
  5. Environment-Specific Alerts: Use different webhooks or customize messages based on the environment.

Remember to keep this document up-to-date as you modify the Lambda function. Regular reviews will ensure that your error filtering and notification process remains effective and aligned with your operational needs.