Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS - subscribe multiple lambda logs to one elasticsearch service

I have two log groups generated by two different lambda. When I subscribe one log group to my elasticsearch service, it is working. However, when I add the other log group I have the following error in the log generated by 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 ?

Thank you.

like image 946
DionysoSong Avatar asked Mar 01 '18 08:03

DionysoSong


People also ask

How do I send Lambda logs to Elasticsearch?

From the “Actions” drop-down, select the appropriate option to set up the log streaming subscription. In the next screen, choose the name of the Elasticsearch domain you created earlier. Next, choose a role that the log streaming Lambda (generated as part of this process) will assume when it executes.

How do I stream Elasticsearch to CloudWatch logs?

Go to the AWS CloudWatch console and click on Logs at the left most; select the CloudTrail Log group that we just created earlier, and click on Actions and select Stream to Amazon Elasticsearch Service.

Does Lambda automatically create log group?

Every time you create a new Lambda function, the Lambda service automatically forms a new log group in CloudWatch Logs.


1 Answers

The problem is that ElasticSearch 6.0.0 made a change that allows indices to only contain a single mapping type. (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.

The default Lambda JS file if created through the AWS console sets the index type to the log group name. An example of the JS file is on this gist (https://gist.github.com/iMilnb/27726a5004c0d4dc3dba3de01c65c575)

Line 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. 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. The name is also set in the @log_group field so queries can use that for filtering.

In my case, I did the following:

  1. Deploy modified 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
  3. Entries from different log groups will now coexist.
like image 113
davidstoker Avatar answered Sep 28 '22 09:09

davidstoker