Alex
Alex

Reputation: 71

AWS Cloudwatch alarm not provisioned correctly. Am I not referencing the log group to filter on correctly?

I am trying to provision in terraform a cloudwatch alarm to trigger on a keyword string printed from a lambda. I can confirm the lambda runs and prints the message for testing purposes. I have confirmed via terminal commands that the cloudwatch alarm exists, but in an insufficient data state. the lambda log group name is "/aws/lambda/error-test-lambda", and I confirmed that test messages are printing successfully.

resource "aws_cloudwatch_metric_alarm" "error-we-care-about-alarm" {
    alarm_name          = "error-we-care-about"
    metric_name         = aws_cloudwatch_log_metric_filter.error-we-care-about-metric-filter.name
    threshold           = 0
    statistic           = "Sum"
    comparison_operator = "GreaterThanThreshold"
    datapoints_to_alarm = "1"
    evaluation_periods  = "1"
    period              = 60
    namespace           = "IncomingLogEvents"
    actions_enabled     = "true"
    alarm_description   = "This metric monitors  API call errors"
}

resource "aws_cloudwatch_log_group" "lamba_test_cloudwatch_logs"{
  name = "/aws/lambda/local-error-test"   
}

resource "aws_cloudwatch_log_metric_filter" "error-we-care-about-metric-filter"{
  name            = "MyErrorFilter-we-care-about"
  log_group_name  = aws_cloudwatch_log_group.lamba_test_cloudwatch_logs.name
  pattern         = "HIPPOPOTAMUS"

  metric_transformation {
    # increment threshold of aws_cloudwatch_metric_alarm 
    name        = "errors-found-alarm"
    namespace   = "mycloudwatchalarmtest"
    value       = 1
  }
}

Upvotes: 2

Views: 670

Answers (1)

Alex
Alex

Reputation: 71

The problem two-fold. The first was I was not referencing the logs from the lambda correctly. The aws_cloudwatch_log_group resource creates a new log, where I am trying to review an existing log to compare my metric filter by. The second was that I was not referencing the aws_cloudwatch_metric_alarm from the aws_cloudwatch_log_metric_filter correctly. I changed how the metric alarm is referenced to be dynamic

The changes would look like this:

resource "aws_cloudwatch_log_metric_filter" "my-error-we-care-about-metric-filter"{
  name            = "MyErrorFilter-we-care-about"
  log_group_name  = "/aws/lambda/error-test-lambda"   
  pattern         = "HIPPOPOTAMUS"

  metric_transformation {
    # increment threshold of aws_cloudwatch_metric_alarm 
    name        = aws_cloudwatch_metric_alarm.error-we-care-about-alarm.metric_name
    namespace   = aws_cloudwatch_metric_alarm.error-we-care-about-alarm.namespace
    value       = 1
    }
}

Upvotes: 2

Related Questions