Skip to main content

cdk-log-notifier: Filter CloudWatch logs and post to Slack.

The AWS CDK Construct to build a system that gather CloudWatch logs, filter and post to Slack.

screenshot

Example Usage

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);

Installation

npm i @thedesignium/cdk-log-notifier

API Reference

Class: LogNotifier

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 like 12/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',
      }
    

Static Method: fromAttributes

LogNotifier.fromAttributes(scope: cdk.Construct, id: string, attrs: LogNotifierAttributes): LogNotifier

Instantiate from the attributes. Put the value of logNotifier.attributes as attrs parameter.

Method: watch

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.

Property: attributes

attributes: LogNotifierAttributes

To use with LogNotifier.fromAttributes().

Containing Resources

Limitation

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.

Motivation

There were 2 requirements:

  1. Notice the all logs produced by console.error()(not only the crash report such InvocationError)
  2. 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.

FAQ

Cross Stack?

Possible. Export all values in LogNotifier.prototype.attributes, import it and use LogNotifier.fromAttributes() in another Stack.

How can I customize the Slack bot icon or name?

You can set at Slack App setting page, or Incoming Webhook configuration page if you use Legacy Incoming Webhook.

No support for other languages than TypeScript?

Supports Python, Java, Go and .NET.

Metadata

Release files for cdk-log-notifier 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdk-log-notifier 2.2.0
File Size Uploaded
cdk-log-notifier-2.2.0.tar.gz 37.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdk-log-notifier 2.2.0
File Interpreter ABI Platform
cdk_log_notifier-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 72.7 kB

Release files / cdk-log-notifier-2.2.0.tar.gz

Download URL cdk-log-notifier-2.2.0.tar.gz
Size 37.2 kB
Tags Source
SHA-256 checksum
How to use checksums
01cfd9a60ec000c66295a497929a83d3d6c2df46eff9b0a75ee7a989a9da6970
BLAKE2b-256 checksum
How to use checksums
e5336e408a704ceec4dd3e12037cab1a6ee59e333e49561a378c5a8edfbcc8eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / cdk_log_notifier-2.2.0-py3-none-any.whl

Download URL cdk_log_notifier-2.2.0-py3-none-any.whl
Size 35.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
542a916c48949462d95b34add7a062ec770344bc22e2c271f5edf6fd23c43ac1
BLAKE2b-256 checksum
How to use checksums
6d9c091656c009d0a87dcd2bf39d12f55536fc8d796f009ad05d30fa2cf2dc00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files

2.0.4

2 release files

2.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page