skip to Main Content

I’m encountering an error while trying to configure an AWS CloudWatch Log Metric Filter using Terraform. My goal is to create a metric filter that captures specific log data based on the level and logger fields. However, I keep getting an "Invalid metric filter pattern" error, and I’m unsure how to correct the pattern syntax.

Here’s my Terraform configuration:

resource "aws_cloudwatch_log_metric_filter" "log_count_metric_filter" {
  name           = "logGroupCount"
  pattern        = "{ $.level = * } { $.logger = <*> }"
  log_group_name = "myLogGroup"

  metric_transformation {
    name      = "logErrorCount"
    namespace = "logSpace"
    value     = "1"
    dimensions = {
      level  = "$.level"
      logger = "$.logger"
      #      message = "$.message"
    }
  }
}

The error I receive is:

Error: putting CloudWatch Logs Metric Filter (logGroupCount: InvalidParameterException: Invalid metric filter pattern
with module.data-retention.aws_cloudwatch_log_metric_filter.log_count_metric_filter
on <> line 21, in resource "aws_cloudwatch_log_metric_filter" "log_count_metric_filter":

I’ve tried several variations of the pattern(pattern = "{$.level $.logger}"
), such as removing commas, changing the structure, but I keep encountering the same error. I’m looking for guidance on how to correctly format this pattern according to AWS CloudWatch Logs filter pattern syntax.

2

Answers


  1. Chosen as BEST ANSWER

    The correct pattern

    pattern        = "{ $.level = * && $.logger = *  }"
    

  2. Probably that will work:

    pattern = "{ ($.level = *) && ($.logger = *) }"
    

    Take a look that tutorial for filters and pattern Syntax: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search