The AWS CDK Construct to build a system that gather CloudWatch logs, filter and post to Slack.
Watch the all logs contains "ERROR" from Lambda functions.
const logNotifier = new LogNotifier(this, 'logNotifier', {
filterPattern: logs.FilterPattern.allTerms('ERROR'),
slackIncomingWebhookUrl: 'https://hooks.slack.com/...', // Use yours.
});
logNotifier.watch(lambdaFunc1.logGroup);
logNotifier.watch(lambdaFunc2.logGroup);
npm i @thedesignium/cdk-log-notifier
new LogNotifier(scope: cdk.Construct, id: string, props: LogNotifierProps)
The properties in props
:
-
filterPattern
: The FilterPattern object in aws-cloudwatch module. The logs is filtered as specified here. Required. -
slackIncomingWebhookUrl
: The Incoming Webhook URL of Slack. Create for the Slack channel the logs should be posted. Required. -
dateTimeFormatOptions
: The arguments of the DateTimeFormat constructor, used to format the datetime which shown at the bottom of the Slack message. If omitted, it's formatted like12/20, 3:00:00 AM UTC
. Optional.Example:
dateTimeFormatOptions: { locales: 'ja-JP', timeZone: 'Asia/Tokyo', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', }
LogNotifier.fromAttributes(scope: cdk.Construct, id: string, attrs: LogNotifierAttributes): LogNotifier
Instantiate from the attributes. Put the value of logNotifier.attributes
as attrs
parameter.
logNotifier.watch(logGroup: logs.LogGroup): void
Add the log group to watch list to notify. The logs in the watched log groups are filtered by the filterPattern and posted to Slack.
attributes: LogNotifierAttributes
To use with LogNotifier.fromAttributes()
.
The watch()
method attaches a Subscription to the subject logGroup. The number of subscription can be attached to a logGroup, however, is only one. So it'll fail if the logGroup has another subscription already. Similary, the watched logGroup can't be attached another subscription nor watched from another LogNotifier.
There were 2 requirements:
- Notice the all logs produced by
console.error()
(not only the crash report such InvocationError) - Jump easily to CloudWatch via link
We tried Lambda's error metric + CloudWatch Alarm + Chatbot
and CloudWatch Metrics Filter + CloudWatch Alarm + Chatbot
, but the former system don't satisfy [1] and the latter system don't satisfy [2]. That's why we need this.
Possible. Export all values in LogNotifier.prototype.attributes
, import it and use LogNotifier.fromAttributes()
in another Stack.
You can set at Slack App setting page, or Incoming Webhook configuration page if you use Legacy Incoming Webhook.
Supports Python, Java, Go and .NET.