Skip to content

Instantly share code, notes, and snippets.

@peacing
Last active October 1, 2020 02:10
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 peacing/9b090ddf64c5dd117c21d26b2aee33c0 to your computer and use it in GitHub Desktop.
Save peacing/9b090ddf64c5dd117c21d26b2aee33c0 to your computer and use it in GitHub Desktop.
query = f"fields @timestamp, @message | " \
"sort @timestamp asc | limit 25 | " \
"filter @requestId = '{request_id}'"
client = boto3.client('logs')
start_query_response = client.start_query(
logGroupName=log_group,
startTime=int((datetime.now() - timedelta(minutes=15)).timestamp()),
endTime=int(datetime.now().timestamp()),
queryString=query,
)
# use query id to poll for results
query_id = start_query_response['queryId']
time.sleep(5)
response = client.get_query_results(
queryId=query_id
)
messages = []
if response['results']:
for log in response['results']:
for field in log:
if field['field'] == '@message':
messages.append(field['value'])
if messages:
# format log messages for slack (newline delimited string)
text = f"*Logs for from failed Lambda invocation*\n```{''.join(m for m in messages)}```"
slack_message = {
'text': text,
"color": "#54C139"
}
webhook_url = 'https://hooks.slack.com/services/example/random/webhook/url'
# send to slack!
requests.post(
url=webhook_url,
data=json.dumps(slack_message),
headers={'Content-Type': 'application/json'}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment