Skip to content

Instantly share code, notes, and snippets.

@iann0036
Created March 6, 2018 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iann0036/8124a293e119291e806ac0305c723d7b to your computer and use it in GitHub Desktop.
Save iann0036/8124a293e119291e806ac0305c723d7b to your computer and use it in GitHub Desktop.
CloudWatch Log Lambda Subscription to ElasticSearch
from __future__ import print_function
import json
import base64
import zlib
from elasticsearch import Elasticsearch
from elasticsearch.connection import create_ssl_context
from datetime import datetime
def handler(event, context):
logs = json.loads(zlib.decompress(base64.b64decode(event['awslogs']['data']), 16+zlib.MAX_WBITS))
context = create_ssl_context(cafile="ca.crt")
es = Elasticsearch(['my.elasticsearch.cluster'], http_auth=('username', 'password'), scheme="https", port=9200, ssl_context=context)
for log_event in logs['logEvents']:
try:
body = json.loads(str(log_event['message']))
except:
body = {
'message': str(log_event['message'])
}
body['lambda_function'] = str(logs['logGroup'])
body['timestamp'] = log_event['timestamp']
body['aws_account'] = '12345678901'
es.index(index="cloudwatchlogs-" + str(datetime.now().strftime("%Y.%m.%d")), doc_type="mymapping", id=log_event['id'], body=body)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment