Git Product home page Git Product logo

cdk-log-notifier's Introduction

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.

cdk-log-notifier's People

Contributors

acomagu avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cdk-log-notifier's Issues

SSM Parameter to hold Slack channel

We would like to hold the Slack webhook URL in a secure SSM parameter so it isn't in source control.

Looks like we can pass a value into Lambda using that:

const lambdaFunction = new lambda.Function(this, 'LogHandler', {
  environment: {
    SLACK_INCOMING_WEBHOOK_URL: ssmParameter.stringValue,
  },
});

Would you accept a PR for that if you agree?

Message redaction feature

We would like to use this to notify on various errors/events, but we know they may contain Personally identifiable information.

Would you be open to a change that would apply (say) a regex group match to extract the part of the message we'd like to notify?

Great project, thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.