避免轮询和长时间运行lambda任务

时间:2019-05-07 13:27:33

标签: aws-lambda

我有一个cloudwatch组,该组监视我的应用程序启动的次数。然后,我编写了一个lamba函数,该函数应该每24小时在python中使用boto3检索一次这些日志。为此,我启动了一个查询,然后轮询get_query_results方法以查看其是否完成。但是,这给我留下了非常糟糕的实现方式,需要大量资源。

是否有更好的方法也许可以使用某种回调来做到这一点?

这是我正在使用的两个功能 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.start_query

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.get_query_results

query_id = cloudwatch_connector.start_query('fields @timestamp, @message|filter @message like /APPSTART/').get('queryId')

# Wait until it is completed
running = True
while running:
    response = cloudwatch_connector.get_query_results(query_id)
    status = response.get('status')
    if status == 'Complete':
        print("Done gathering logs")
        print(response)
        running = False
    if status == 'Failed' or status == 'Cancelled':
        raise Exception('Request either failed or was cancelled')
    time.sleep(1000)

我真正想做的是运行一个Insights查询并将其每晚每晚00:00保存到一个文本文件中

0 个答案:

没有答案