I want to send Lambda error notifications to Slack using AWS Chatbot

0

Hello.

We are currently thinking of using AWS Chatbot to send Lambda error notifications to Slack.

Currently my Lambda is set up to notify me via email via SNS when an error occurs with the settings described in the following document.
Can I send messages to Slack just by linking AWS Chatbot to this SNS subscription?
https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-features.html#gettingstarted-features-destinations

If not, do I need to create a CloudWatch Logs metrics filter, create custom metrics for error occurrences, set up alarms, tie in SNS, and notify with AWS Chatbot?

The following document lists the services that can be used for notifications, but if Lambda is used directly, is it correct that AWS Chatbot cannot be used?
https://docs.aws.amazon.com/ja_jp/chatbot/latest/adminguide/related-services.html

2 Answers
2
Accepted Answer

Hi Riku,

If I understand it correctly, you have configured a Lambda function with a SNS topic as an "on failure" destination. Now, you would like to know if it is sufficient to link the AWS Chatbot to this SNS topic in order to receive these events in your Slack channel, correct?

Unfortunately, this type of event is (currently) not supported. You would see these as "UnsupportedEvents" in the AWS Chatbot CloudWatch metrics, more information is available here.

As you have noted, creating a CloudWatch alarm would however work.

profile pictureAWS
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 25 days ago
profile pictureAWS
EXPERT
reviewed 10 months ago
  • Thank you for your response. I would like to create custom metrics using the CloudWatch Logs metrics filter and set it up to alert me when an error occurs.

0

I think there is a way to do it, it's a bit hacky but it works well.

In this example we're intercepting an ECS Event and filtering out specific patterns which aren't possible with EventBridge Rules. Then we create a custom event and pass it to SNS which is subscribed by AWS Chatbot. The format is event creation that's what matters createCustomMessage

import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:xxxxxxxxxx:proj-dev-purpose-slackbot';

export const handler = async (event) => {
    const ecsEvent = typeof event === 'object' ? event : JSON.parse(event);

    // Check if the event matches your criteria
    if (isMatchingEvent(ecsEvent)) {
        // Publish to the specified SNS topic
        const message = createCustomMessage(event);
        await publishToSns(SNS_TOPIC_ARN, message);
    }
};

function createCustomMessage(event) {
    return JSON.stringify({
        "version": "0",
        "id": event.id,
        "detail-type": "ECS Task Out Of Memory",
        "source": "aws.events",
        "account": event.account,
        "time": event.time,
        "region": event.region,
        "resources": [
            `\`Container\`: ${ event.detail.containers[0].name }`,
            `\`Group\`: ${ event.detail.group }`,
            `\`Reason\`: ${ event.detail.containers[0].reason }`,
            `\`Desired State\`: ${ event.detail.desiredStatus }`,
            `\`Memory\`: ${ event.detail.memory }`,
            `\`CPU\`: ${ event.detail.cpu }`,
            `\`Created At\`: ${ event.detail.createdAt }`,
        ],
        "detail": {}
    }, null, 2);
}

function isMatchingEvent(event) {
    console.log('event', event);
    const desiredReason = "OutOfMemoryError: Container killed due to memory usage";

    const desiredFormatEvent = event;

    // Check if the event matches the criteria
    return (
        desiredFormatEvent?.detail?.containers?.length &&
        desiredFormatEvent.detail.containers.find(container => container.lastStatus === 'STOPPED' && container.reason === desiredReason)
    );
}

async function publishToSns(topicArn, event) {
    const snsPublishCommand = new PublishCommand({
        TopicArn: topicArn,
        Message: event,
        Subject: 'Out of Memory ECS Task'
    });

    const snsClient = new SNSClient();
    try {
        const result = await snsClient.send(snsPublishCommand);
        console.log("Message published to SNS:", result);
    } catch (error) {
        console.error("Error publishing message to SNS:", error);
    }
}

profile picture
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions