Last active
October 28, 2015 17:28
-
-
Save jamiegs/36e66b068fb7baaadc9a to your computer and use it in GitHub Desktop.
VPC to Sumologic Lambda Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Jesse' | |
import urllib2 | |
import base64 | |
import zlib | |
import json | |
import StringIO | |
import gzip | |
def lambda_handler(event, context): | |
data = event.get('awslogs').get('data') | |
#url we are posting to | |
url = '' | |
try: | |
#convert from base 64 | |
decoded_string = base64.b64decode(data) | |
decompressed_data=json.loads(zlib.decompress(decoded_string, 16+zlib.MAX_WBITS)) | |
#pull out the log records and append them to a new line on a string | |
temp_str ='' | |
for rec in decompressed_data['logEvents']: | |
temp_str += json.dumps(rec)+"\n" | |
#re-gzip the files | |
out = StringIO.StringIO() | |
with gzip.GzipFile(fileobj=out, mode="w") as f: | |
f.write(temp_str) | |
#setup the web request | |
method = "POST" | |
handler = urllib2.HTTPHandler() | |
opener = urllib2.build_opener(handler) | |
request = urllib2.Request(url, data=out.getvalue()) | |
request.add_header("Content-Encoding",'gzip') | |
request.get_method = lambda: method | |
#post to the url | |
try: | |
connection = opener.open(request) | |
except urllib2.HTTPError,e2: | |
connection = e2 | |
print e2 | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment