Last active
June 12, 2018 19:01
-
-
Save fcavalcantirj/cd43289725faa56c8a3403db5842f46d to your computer and use it in GitHub Desktop.
lambda function to export all logs from cloudWatch logGroup to s3Bucket. PS. Remember to set perms accordingly and to rename logGroupName, destinationPrefix, etc...
This file contains 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
"""This is an function to export ALL logs from a logGroup to a s3 bucket. | |
Usage:: | |
Just create a new lambda function and paste this code. | |
I also created a scheduled event with rate(1 hour) | |
""" | |
from __future__ import print_function | |
import boto3 | |
import botocore.exceptions | |
import hmac | |
import hashlib | |
import base64 | |
import json | |
import uuid | |
import logging | |
from datetime import tzinfo, timedelta | |
import datetime | |
client = None | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
class FixedOffset(tzinfo): | |
def __init__(self, offset): | |
self.__offset = timedelta(hours=offset) | |
self.__dst = timedelta(hours=offset-3) | |
self.__name = '' | |
def utcoffset(self, dt): | |
return self.__offset | |
def tzname(self, dt): | |
return self.__name | |
def dst(self, dt): | |
return self.__dst | |
def exportToS3(fromTime, to): | |
try: | |
resp = client.create_export_task( | |
taskName='LogTask_'+datetime.datetime.now(FixedOffset(-3)).strftime("%Y-%m-%d %H:%M:%S"), | |
logGroupName='/aws/lambda/Something', | |
fromTime=int(round(fromTime.timestamp() * 1000)), | |
to=int(round(to.timestamp() * 1000)), | |
destination='s3BucketName', | |
destinationPrefix='somePreffixConfiguredOnS3Bucket' | |
) | |
except Exception as e: | |
#print(e) | |
logger.error(e) | |
return None, "Unknown error" | |
return resp, None | |
def lambda_handler(event, context): | |
global client | |
if client == None: | |
client = boto3.client('logs') | |
#print(event) | |
fromTime = datetime.datetime.now(FixedOffset(-3)) - datetime.timedelta(minutes=60) | |
to = datetime.datetime.now(FixedOffset(-3)) | |
resp, msg = exportToS3(fromTime, to) | |
if msg != None: | |
# return {'status': 'fail', 'msg': msg} | |
logger.info('failed to export logs to s3') | |
raise Exception(msg) | |
logger.info('successfully exported cloudWatchLogs...') | |
return {'status': 'success'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment