Skip to content

Instantly share code, notes, and snippets.

@oremj
Created November 12, 2019 16:08
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 oremj/a09c2b5525f090e4cd0d8535d67928d9 to your computer and use it in GitHub Desktop.
Save oremj/a09c2b5525f090e4cd0d8535d67928d9 to your computer and use it in GitHub Desktop.
watch_cloudwatch_logs.py
#!/usr/bin/env python3
import click
import boto3
import time
logs_client = boto3.client('logs')
@click.command()
@click.option("--group", type=str)
@click.option("--query", type=str)
@click.option("--start", type=int, default=86400)
@click.option("--end", help='start and end are relative', type=int, default=0)
def query_logs(group, start, end, query):
now = int(time.time())
start_time = now - start
end_time = now - end
query_id = logs_client.start_query(
logGroupName=group,
startTime=start_time,
endTime=end_time,
queryString=query,
)['queryId']
resp = None
while True:
resp = logs_client.get_query_results(queryId=query_id)
if resp['status'] == 'Running':
time.sleep(1)
continue
break
results = resp['results']
for result in results:
result = {field['field']: field['value'] for field in result}
print(f"{result['@timestamp']} {result['@message']}".strip())
if __name__ == "__main__":
query_logs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment