AWS Lambda 函数 Boto 3 filter_log_events 无法使用 endTime 参数

AWS Lambda Function Boto 3 filter_log_events Cannot Use endTime Parameter

我正在尝试从 Cloudwatch 日志中提取信息,以根据指标从警报中发送更加自定义的电子邮件。我正在创建一个 lambda 函数来尝试在指定的时间范围内提取信息。但是,当我指定 startTime 和 endTime 时,响应中没有任何结果。我查看了 Boto3 的文档,但内容不多。我对 python 和 AWS 比较陌生,所以任何帮助都会很棒。代码示例如下所示:

import boto3
import json
import time
from datetime import datetime
from calendar import timegm

# Create CloudWatch client
clw = boto3.client('logs')


def lambda_handler(event, context):

    # User defined for testing purposes
    name = 'Error Log Metric'
    namespace = 'User Defined Metrics'

    response = clw.describe_metric_filters(metricName=name,
            metricNamespace=namespace)

    LogGroupName = response['metricFilters'][0]['logGroupName']
    FilterPattern = response['metricFilters'][0]['filterPattern']

    StartTime = timegm(time.strptime('2020-06-15T00:00:00.000Z',
                       '%Y-%m-%dT%H:%M:%S.%fZ'))
    EndTime = timegm(time.strptime('2020-06-16T23:59:59.000Z',
                     '%Y-%m-%dT%H:%M:%S.%fZ'))

    filteredLogs = clw.filter_log_events(logGroupName=LogGroupName,
            filterPattern=FilterPattern,
            logStreamNamePrefix='TEST_PREFIX_NAME',
            startTime=StartTime, endTime=EndTime)

    print(filteredLogs)

回应

Response:
null

Request ID:
"610a2849-3fef-46b2-b75e-450c4f37ec25"

Function Logs:
START RequestId: 610a2849-3fef-46b2-b75e-450c4f37ec25 Version: $LATEST
{'events': [], 'searchedLogStreams': [], 'ResponseMetadata': {'RequestId': '04d59cfe-9069-4bb4-ad3b-7135a649d2e6', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '04d59cfe-9069-4bb4-ad3b-7135a649d2e6', 'content-type': 'application/x-amz-json-1.1', 'content-length': '121', 'date': 'Tue, 16 Jun 2020 00:42:23 GMT'}, 'RetryAttempts': 0}}
END RequestId: 610a2849-3fef-46b2-b75e-450c4f37ec25
REPORT RequestId: 610a2849-3fef-46b2-b75e-450c4f37ec25  Duration: 299.88 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 71 MB  Init Duration: 315.90 ms

我认为问题是 timegm returns 时间戳 ,而不是 毫秒 filter_log_events.

假设其他一切都正确(所有日志流名称、日期、前缀),快速修复可能是:

    filteredLogs = clw.filter_log_events(logGroupName=LogGroupName,
            filterPattern=FilterPattern,
            logStreamNamePrefix='TEST_PREFIX_NAME',
            startTime=StartTime*1000, endTime=EndTime*1000)