Skip to content

Instantly share code, notes, and snippets.

@jamiegs
Last active October 28, 2015 17:28
Show Gist options
  • Save jamiegs/36e66b068fb7baaadc9a to your computer and use it in GitHub Desktop.
Save jamiegs/36e66b068fb7baaadc9a to your computer and use it in GitHub Desktop.
VPC to Sumologic Lambda Function
__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