- Más nuevo
- Más votos
- Más comentarios
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.
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);
}
}
Contenido relevante
- OFICIAL DE AWSActualizada hace 2 años
- OFICIAL DE AWSActualizada hace 4 meses
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.