如何使用Terraform基于Cloudwatch日志指标过滤器将Cloudwatch警报发送到SNS

时间:2019-03-26 22:03:13

标签: terraform amazon-cloudwatch amazon-cloudwatch-metrics

我正在测试cloudwatch警报筛选器(以精确检查lambda是否在内存中达到最大值),以发送到SNS,然后将其发送到SQS队列。但是,我看不到过滤器出现在日志中。 设置为cloudwatch (filtered alarm) -> SNS -> SQS ->splunk

到目前为止我所拥有的:

resource "aws_cloudwatch_metric_alarm" "general_lambda_error" {
    depends_on = [
      "aws_cloudwatch_log_metric_filter.max_memory_time_out",
    ]   
    alarm_name                = "general_lambda_error"
    comparison_operator       = "GreaterThanOrEqualToThreshold" 
    evaluation_periods        = "1" 
    metric_name               = "Errors" 
    namespace                 = "AWS/Lambda" 
    period                    = "60" 
    statistic                 = "SampleCount" 
    threshold                 = "2"
    alarm_description         = "This metric monitors Lambda Memory Max Usage and other Errors: threshold=2"
    alarm_actions             = [ "some-arn" ]
    dimensions {
      FunctionName = "lambda-test"
      Resource = "lambda-test"
    }
}
resource "aws_cloudwatch_log_metric_filter" "max_memory_time_out" {
    name                      = "max_memory_time_out"
    pattern                   = "[report_name=\"REPORT\", 
  request_id_name=\"RequestId:\", request_id_value, duration_name=\"Duration:\", duration_value, duration_unit=\"ms\", billed_duration_name_1=\"Billed\", bill_duration_name_2=\"Duration:\", billed_duration_value, billed_duration_unit=\"ms\", memory_size_name_1=\"Memory\", memory_size_name_2=\"Size:\", memory_size_value, memory_size_unit=\"MB\", max_memory_used_name_1=\"Max\", max_memory_used_name_2=\"Memory\", max_memory_used_name_3=\"Used:\", max_memory_used_value, max_memory_used_unit=\"MB\"]"
    log_group_name            = "/aws/lambda/lambda-test"
    metric_transformation {
      name      = "SampleCount"
      namespace = "cloudwatch_filter"
      value     = "1"
    }
}

如何发送已过滤的邮件?我发现这个link描述了类似的问题,但是解决方案是创建一个lambda函数。我可以在不创建lambda函数的情况下做到吗?

1 个答案:

答案 0 :(得分:0)

这是将cloudwatch日志获取到S3的代码:

functions:
  process:
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'