Aws_cloudwatch _log_metric_filter custom metric dimensions

I am creating custom metrics from metric filters.

I see that recently AWS released support for dimensions for custom metrics. Doc: Amazon CloudWatch Logs announces Dimension support for Metric Filters.

The documentation states that you can specify dimension as a map under metric transformation in the aws_cloudwatch_log_metric_filter module.
Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_metric_filter#dimensions

Here is what I have in my template:

data "aws_cloudwatch_log_group" "authentication-service-api-log-group" {
  name = "/${local.env}/authentication-service-${local.env}-api"
}

resource "aws_cloudwatch_log_metric_filter" "authentication-service-api-metric-filter" {
  name           = "authentication-service-${local.env}-api-level50-errors"
  pattern        = "{$.level = 50}"
  log_group_name = data.aws_cloudwatch_log_group.authentication-service-api-log-group.name

  metric_transformation {
    name       = "Level50Errors"
    namespace  = "CustomMetrics"
    value      = "1"
    dimensions = {
      Project = join("-", ["authentication-service", local.env, "api"])
    }
  }
}

This is the error I am getting:
Error: Creating/Updating CloudWatch Log Metric Filter failed: InvalidParameterException: Invalid metric transformation: dimension value must be valid selector

What is a valid selector? How do I use the dimensions argument?

2 Likes

Iā€™m experiencing the same issue.

Hi @captain-kirk ,

I just ran into the same issue. It seems that the dimensions have to reference some property of your filter pattern.

resource "aws_cloudwatch_log_metric_filter" "log_events_count" {
  log_group_name = var.log_group_name
  name = "log.events.count"
  pattern = "{ $.level = * }"
  metric_transformation {
    namespace = var.namespace
    name = "log.events.count"
    dimensions = {
      level = "$.level"
    }
    value = "1"
  }
}

is a metric filter I just created and it seems to work fine.

1 Like