question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[lambda] expose log group and support metric filter

See original GitHub issue

🚀 Feature Request

General Information

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

Description

Currently it’s impossible to add MetricFilter for Lambda logs, because Lambda does not return log group and log group will be created only after any invocations of function. Manually created log groups (in CDK) might not work because of possible race conditions.

Addition of MetricFilter to logs is similar to LogGroup retention setting as discussed here https://github.com/aws/aws-cdk/issues/667#issuecomment-474002870 and implemented in PR #2067

Proposed Solution

Environment

  • CDK CLI Version: all
  • Module Version: all
  • OS: all
  • Language: all

Other information

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mariusGundersencommented, Jan 13, 2020

There seems to be two ways to do this:

  • create a new logGroup()
  • get an existing logGroup using LogGroup.fromLogGroupArn()

The first method only works if the log group doesn’t already exist, but the second one only works if the log group already exists. In my scenario I had made the lambda (and the log group) in a previous deploy, and then I wanted to add a subscription to the log group in a second deploy, so I had to use the second variant. But then I tried to deploy this construct in a new stack, and now it failed, since the log group didn’t exist yet. So Unless you know the state of the stack you are deploying to you can’t really use these.

2reactions
AdonousTechcommented, Jan 2, 2020

@nija-at I take that back, I can actually create the log group. I’m not sure what I did wrong last time when I received the naming error.

After working through this, it seems that I now have an acceptable flow. In my TS code I simply 1) declare the LogGroup after the lambda.Function and follow the standard naming convention /aws/lambda/' + this.functionName. 2) I reference the log group on the MetricFilter by simply using this.logGroup. This order seems to work (e.g. lambda function first, log group second).

Problem solved. Thanks! For anyone else struggling with this:

      // first declare lambda function
      this.lambdaFunction = new lambda.Function(this, Vars.lambda.functionName, {
        runtime: lambda.Runtime.NODEJS_12_X,
        handler: 'index.handler',
        tracing: lambda.Tracing.ACTIVE,
        role: this.executionRole,
        code: lambda.Code.fromAsset(path.join(__dirname, '../../lambda/code/skill_lambda.zip'))
      });

      // declare log group using standard naming convention
      this.logGroup = new LogGroup(this, 'LogGroup', {
        logGroupName: '/aws/lambda/' + this.lambdaFunction.functionName
      })

      // declare other dependencies on log group such as metric filter, alarms, etc
            this.metricFilter = new logs.MetricFilter(this, 'MetricFilter', {
        filterPattern: {logPatternString: '{ $.eventType = "CriticalError" }'},
        logGroup: this.logGroup,
        metricNamespace: this.metric.namespace,
        metricName: this.metric.metricName,
        defaultValue: 0,
        metricValue: '1'
      });


Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a metric filter for a log group - Amazon CloudWatch Logs
How to create a metric filter that publishes a metric to CloudWatch based on the ... In the navigation pane, choose Logs, and...
Read more >
Ensure a log metric filter and alarm exist for changes to NACLs
Monitoring changes to NACLs will help ensure that AWS resources and services are not unintentionally exposed. We recommend you establish a log metric...
Read more >
Sending CloudWatch Custom Metrics From Lambda ... - Stackify
Today's post is a detailed guide on how to send custom metrics to AWS CloudWatch monitoring using AWS Lambda. Starting from scratch, we're ......
Read more >
Amazon CloudWatch Logs and AWS Lambda - YouTube
Learn more about AWS at - http://amzn.to/2zdfmIe.In this session, we cover three common scenarios that include Amazon CloudWatch Logs and ...
Read more >
Monitoring AWS Lambda Functions With CloudWatch
We will use existing metrics but also create a custom metric filter to parse the memory consumption from CloudWatch logs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found