Error Filter Discord
📋 Table of Contents
- Overview
- Function Structure
- Key Components
- Environment Variables
- Error Handling
- Best Practices
- 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:
- Main Handler: Processes incoming CloudWatch Logs events.
- Decoding and Decompression: Extracts log data from the CloudWatch event.
- Discord Payload Creation: Formats log messages 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)
};
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
- Secure Webhook URL: Always store the Discord webhook URL as an environment variable, never hardcode it.
- Limit Message Size: Discord has a character limit for messages. Ensure your payloads don't exceed this limit.
- 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: Use different webhooks or customize messages based on the environment.
🔗 Useful Links
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.