从日志发送 cloudwatch 电子邮件

Sending cloudwatch email from logs

我正在使用 EC2 实例。我已将应用程序中的异常记录到 cloudwatch 日志中。 日志格式为 JSON object/string:

{
'application': 'abc',
'type': '404',
'error': 'The page you requested was not found.'
}

现在我想根据日志类型将特定日志通过电子邮件发送到电子邮件地址,例如:仅类型为 404 的日志。 我怎样才能做到这一点?

我没有将 SNS 视为对日志组的订阅,但您可以使用 Lambda 函数执行此操作。

当日志匹配时,触发 lambda,您可以从 lambda 发送电子邮件或根据消息执行任何操作。

在订阅中添加过滤器。

aws logs put-subscription-filter --log-group-name /aws/ec2/execption --destination-arn arn:aws:lambda:us-east-1:123456:function:send-email-on-exception --filter-name ec2-404-errors --filter-pattern "404"

过滤器和模式语法

You can use metric filters to search for and match terms, phrases, or values in your log events. When a metric filter finds one of the terms, phrases, or values in your log events, you can increment the value of a CloudWatch metric. For example, you can create a metric filter to search for and count the occurrence of the word ERROR in your log events.

FilterAndPatternSyntax

您可以浏览此 article 以阅读 aws CW 日志事件。