【问题标题】:How to make a lambda listen on multiple log groups of cloudwatch?如何让 lambda 监听多个 cloudwatch 日志组?
【发布时间】:2021-01-06 17:42:16
【问题描述】:

我有一个处理来自 cloudwatch 日志组的日志的 lambda。它将日志保存到 Elasticsearch 集群。我正在使用无服务器配置日志组流式传输到 lambda:https://www.serverless.com/framework/docs/providers/aws/events/cloudwatch-log/

但是,它只支持监听一个日志组。如何让我的 lambda 监听多个日志组?我想使用如下模式:

functions:
  myCloudWatchLog:
    handler: myCloudWatchLog.handler
    events:
      - cloudwatchLog: '/aws/lambda/hello*'

在上面的示例中,我希望每当有日志发送到名称以/aws/lambda/hello 开头的日志组时触发我的 lambda。这样,它会将所有日志保存到 Elasticsearch 中进行分析。

我无法在日志组上添加通配符,如果我添加*会出现此错误

An error occurred (InvalidParameterException) when calling the PutSubscriptionFilter operation: 1 validation error detected: Value '/aws/lambda/hello*' at 'logGroupName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\.\-_/#A-Za-z0-9]+

【问题讨论】:

  • 我希望我能正确理解这个问题。让你试试日志订阅过滤器。 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
  • 过滤器只在一个日志组内起作用。我无法按日志组名称过滤。
  • 不确定是否可以使用通配符模式,但您应该能够在events 下添加多个- cloudwatchLog: '/aws/lambda/hello' 条目。
  • 此处不允许使用通配符。

标签: amazon-web-services aws-lambda amazon-cloudwatch serverless-framework


【解决方案1】:

您可以添加多个cloudwatchLog 事件

  myCloudWatchLog:
    handler: myCloudWatchLog.handler
    events:
      - cloudwatchLog: '/aws/lambda/hello1'
      - cloudwatchLog: '/aws/lambda/hello2'

如果你想为每个日志组赋予不同的过滤模式,你可以使用:

  myCloudWatchLog:
    handler: myCloudWatchLog.handler
    events:
      - cloudwatchLog: 
          logGroup: '/aws/lambda/hello1'
          filter: 'filter1'
      - cloudwatchLog: 
          logGroup: '/aws/lambda/hello2'
          filter: 'filter2'

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 2018-12-12
    • 2020-05-13
    • 2018-05-06
    • 2016-11-27
    • 1970-01-01
    • 2020-02-17
    • 2016-03-22
    • 1970-01-01
    相关资源
    最近更新 更多