0
votes

I'm trying to create a custom metric using CloudFormation. I've followed the example from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html. My existing lambda logs to a CloudWatch log group that shows up in CloudWatch as /aws/lambda/my-function-name. Here's my CloudFormation YAML for the metric:

  ErrorsLogMetric:
    Type: AWS::Logs::MetricFilter
    Properties:
      LogGroupName: !Sub "/aws/lambda/${AWS::StackName}"
      FilterPattern: "[ERROR]"
      MetricTransformations:
        - MetricValue: "1"
          MetricNamespace: "LogMetrics"
          MetricName: "MyCustomMetric"

${AWS::StackName} resolves to "my-function-name" when it runs. The CloudFormation script runs successfully and says the metric was created, but when I go to CloudWatch the log group for my lambda, it shows zero filters. What do I need to do differently to cause this custom metric to show up as a filter for my lambda log group when it is created via CloudFormation?

If I hard code the property as LogGroupName: "/aws/lambda/my-function-name" then it works. But I don't want to hard code it since the value of ${AWS::StackName} is dynamic in different use cases.

1
Your template looks right. If the log group was wrong the create would fail. It's possible that the display didn't update. How long after it ran did you look at the log groups?Jason Wadsworth
Have you tried using cli's describe-metric-filters to check if filter is there? I think I had similar experience to you, and could not find filters in console, after creating them in cloudfront.Marcin
Are you creating the log group in your template? If so, change the LogGroupName to be !Ref LogGroupLogicalId.Jason Wadsworth

1 Answers

1
votes

It seemed to be for me the solution was to add

DependsOn: LambdaLogGroup

where LambdaLogGroup was the resource defined earlier in my template. It threw me off because the log group had already existed for some time, and I was attempting to update the stack and add the metric. Something about that still required the dependency even though the log group already existed and was not updated by CloudFormation when I added the Metric.