简体   繁体   English

AWS - 将多个 lambda 日志订阅到一个 elasticsearch 服务

[英]AWS - subscribe multiple lambda logs to one elasticsearch service

I have two log groups generated by two different lambda.我有两个由两个不同的 lambda 生成的日志组。 When I subscribe one log group to my elasticsearch service, it is working.当我将一个日志组订阅到我的 elasticsearch 服务时,它正在工作。 However, when I add the other log group I have the following error in the log generated by cloudwatch :但是,当我添加另一个日志组时,我在 cloudwatch 生成的日志中出现以下错误:

"responseBody": "{\"took\":5,\"errors\":true,\"items\":[{\"index\":{\"_index\":\"cwl-2018.03.01\",\"_type\":\"/aws/lambda/lambda-1\",\"_id\":\"33894733850010958003644005072668130559385092091818016768\",\"status\":400,\"error\":
{\"type\":\"illegal_argument_exception\",\"reason\":\"Rejecting mapping update to [cwl-2018.03.01] as the final mapping would have more than 1 type: [/aws/lambda/lambda-1, /aws/lambda/lambda-2]\"}}}]}"

How can I resolve this, and still have both log group in my Elasticsearch service, and visualize all the logs ?我该如何解决这个问题,并且在我的 Elasticsearch 服务中仍然有两个日志组,并可视化所有日志?

Thank you.谢谢。

The problem is that ElasticSearch 6.0.0 made a change that allows indices to only contain a single mapping type.问题是 ElasticSearch 6.0.0 做了一个改变,允许索引只包含一个映射类型。 ( https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html ) I assume you are running an ElasticSearch service instance that is using version 6.0. ( https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html ) 我假设您正在运行使用 6.0 版的 ElasticSearch 服务实例。

The default Lambda JS file if created through the AWS console sets the index type to the log group name.如果通过 AWS 控制台创建,默认 Lambda JS 文件会将索引类型设置为日志组名称。 An example of the JS file is on this gist (https://gist.github.com/iMilnb/27726a5004c0d4dc3dba3de01c65c575 ) JS 文件的一个例子在这个要点上(https://gist.github.com/iMilnb/27726a5004c0d4dc3dba3de01c65c575

Line 86: action.index._type = payload.logGroup;第 86 行: action.index._type = payload.logGroup;

I personally have a modified version of that script in use and changed that line to be:我个人使用了该脚本的修改版本,并将该行更改为:

action.index._type = 'cwl';

I have logs from various different log groups streaming through to the same ElasticSearch instance.我有来自各种不同日志组的日志流到同一个 ElasticSearch 实例。 It makes sense to have them all be the same type since they are all CloudWatch logs versus having the type be the log group name.将它们全部设为相同类型是有意义的,因为它们都是 CloudWatch 日志,而不是将类型设为日志组名称。 The name is also set in the @log_group field so queries can use that for filtering.该名称也在@log_group字段中设置,以便查询可以使用它进行过滤。

In my case, I did the following:就我而言,我执行了以下操作:

  1. Deploy modified Lambda部署修改后的 Lambda
  2. Reindex today's index ( cwl-2018.03.07 for example) to change the type for old documents from <log group name> to cwl重新索引今天的索引(例如cwl-2018.03.07 ),将旧文档的类型从<log group name>更改为cwl
  3. Entries from different log groups will now coexist.来自不同日志组的条目现在将共存。

You can also modify the generated Lambda code like below to make it work with multiple CW log groups.您还可以修改生成的 Lambda 代码,如下所示,使其适用于多个 CW 日志组。 If the Lambda function can create different ES index for the different log streams coming under the same log groups, then we can avoid this problem.如果 Lambda 函数可以为同一日志组下的不同日志流创建不同的 ES 索引,那么我们就可以避免这个问题。 So, you need to find the Lambda function LogsToElasticsearch_<AWS-ES-DOMAIN-NAME> , then the function function transform(payload) , and finally change the index name formation part like below.因此,您需要找到 Lambda 函数LogsToElasticsearch_<AWS-ES-DOMAIN-NAME> ,然后是函数function transform(payload) ,最后更改索引名称形成部分,如下所示。

    // index name format: cwl-YYYY.MM.DD
    //var indexName = [
        //'cwl-' + timestamp.getUTCFullYear(),              // year
        //('0' + (timestamp.getUTCMonth() + 1)).slice(-2),  // month
        //('0' + timestamp.getUTCDate()).slice(-2)          // day
    //].join('.');

    var indexName = [
        'cwl-' + payload.logGroup.toLowerCase().split('/').join('-') + '-' + timestamp.getUTCFullYear(),              // log group + year
        ('0' + (timestamp.getUTCMonth() + 1)).slice(-2),  // month
        ('0' + timestamp.getUTCDate()).slice(-2)          // day
    ].join('.');

Is it possible to forward all the cloudwatch log groups to a single index in ES?是否可以将所有 cloudwatch 日志组转发到 ES 中的单个索引? Like having one index "rds-logs-* "to stream logs from all my available RDS instances.就像拥有一个索引“rds-logs-*”来从我所有可用的 RDS 实例流式传输日志。 example: error logs, slow-query logs, general logs, etc., of all RDS instances, would be required to be pushed under the same index(rds-logs-*)?例如:所有RDS实例的错误日志、慢查询日志、一般日志等,是否需要推送到同一个索引(rds-logs-*)下?

I tried the above-mentioned code change, but it pushes only the last log group that I had configured.我尝试了上述代码更改,但它只推送我配置的最后一个日志组。

From AWS: by default, only 1 log group can stream log data into ElasticSearch service.来自 AWS:默认情况下,只有 1 个日志组可以将日志数据流式传输到 ElasticSearch 服务中。 Attempting to stream two log groups at the same time will result in log data of one log group override the log data of the other log group.尝试同时流式传输两个日志组将导致一个日志组的日志数据覆盖另一个日志组的日志数据。

Wanted to check if we have a work-around for the same.想检查我们是否有相同的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM