Skip to content

Instantly share code, notes, and snippets.

@alfallouji
Last active June 8, 2021 09:51
Show Gist options
  • Save alfallouji/5ad1a77ff81f6bf2895f1c6824173016 to your computer and use it in GitHub Desktop.
Save alfallouji/5ad1a77ff81f6bf2895f1c6824173016 to your computer and use it in GitHub Desktop.
def query_aws_cloudwatch(query, log_group, region):
# Get a client
client = boto3.client('logs', region_name=region)
# Launch a query
start_query_response = client.start_query(
logGroupName=log_group,
startTime=int((datetime.today() - timedelta(hours=5)).timestamp()),
endTime=int(datetime.now().timestamp()),
queryString=query,
)
# Check if query has been completed
query_id = start_query_response['queryId']
response = None
while response == None or response['status'] == 'Running':
print('Waiting for query to complete ...')
time.sleep(1)
response = client.get_query_results(
queryId=query_id
)
return response
@alfallouji
Copy link
Author

Added comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment