Created
February 11, 2019 18:25
-
-
Save okram999/82b5c6e60d1123a74df74179c6bd63b0 to your computer and use it in GitHub Desktop.
parse_cwlogs_sns_notification
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
import json | |
import boto3 | |
import time | |
# recieve the event | |
# Contruct the log_stream | |
# pull the log | |
# send notification | |
def get_log_events(log_stream_name): | |
""" | |
Generate all the log events from a CloudWatch log stream for the last 30 mins. | |
""" | |
e_time = int(time.time()) | |
end_time = e_time * 1000 | |
print(f"end_time is : {end_time}") | |
start_time = end_time - 300000 | |
print(f"start_time is : {start_time}") | |
# start_time = 1549776979000 | |
# end_time = 1549777848000 | |
client = boto3.client('logs') | |
kwargs = { | |
'logGroupName': 'some_name_replace_it', | |
'limit': 1, | |
'logStreamNames': [ | |
log_stream_name, | |
], | |
'startTime': start_time, | |
'endTime': end_time, | |
} | |
while True: | |
resp = client.filter_log_events(**kwargs) # unpack the dict kwargs | |
yield from resp['events'] | |
try: | |
kwargs['nextToken'] = resp['nextToken'] | |
except KeyError: | |
break | |
def publishMsg(sns_data, service_name, cluster_name): | |
client = boto3.client('sns') | |
print("Sending email notification......") | |
print(sns_data) | |
# try: | |
response = client.publish( | |
TopicArn= | |
'arn:aws:sns:us-east-1:xxxxxxxxxx', | |
Message=sns_data, | |
Subject= f"{service_name} failed in {cluster_name}", | |
MessageStructure='json') | |
print(response) | |
# except: | |
# print("Email notification failed!") | |
def lambda_handler(event, context): | |
task = event["detail"]["taskDefinitionArn"].split('/')[1].split(':')[0] | |
cluster_name = "-".join(task.split("-", 5)[:5]) | |
service_name = event["detail"]["containers"][0]["name"] | |
task_id = event["detail"]["taskArn"].split('/')[1] | |
log_stream = cluster_name + '/' + service_name + '/' + task_id | |
print (log_stream) | |
allresults = '' | |
for data in get_log_events(log_stream): | |
allresults += data['message'] | |
print("Printing all results:.....") | |
print(allresults) | |
sns_payload = { | |
'task': task, | |
'cluster_name': cluster_name, | |
'service_name': service_name, | |
'task_id': task_id, | |
'log_stream': log_stream, | |
'allresults': allresults[-300:] | |
} | |
# sns_payload = {"foo": "bar"} | |
json_message = json.dumps({"default": json.dumps(sns_payload)}) | |
publishMsg(json_message, service_name, cluster_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment