“Lambda”对象没有属性“filter_log_events”

问题描述 投票:0回答:1

当我尝试迭代所有 lambda 以获得最后执行/调用日期和时间时,收到以下错误。

“Lambda”对象没有属性“filter_log_events”

def get_last_run_time(function_name):
    client = boto3.client('lambda')

    try:
        log_group = f"/aws/lambda/{function_name}"
        log_response = client.filter_log_events(logGroupName=log_group, orderBy='timestamp', descending=True, limit=1)
        if log_response['events']:
            last_invocation_time = log_response['events'][0]['timestamp']
        else:
            last_invocation_time = "No recent invocations"
        return last_modified
    except Exception as e:
        print(f"Error getting last run time for {function_name}: {e}")
        return e
python aws-lambda boto3
1个回答
0
投票

发生该错误是因为 boto3.client('lambda') 对象中没有 filter_log_events 方法。 filter_log_events 方法是 CloudWatch Logs API 的一部分。

您应该使用 boto3.client('logs') 与 CloudWatch Logs 交互,并从与 Lambda 函数关联的日志组中过滤事件,如下所示:

import boto3

def get_last_run_time(function_name):
    logs_client = boto3.client('logs') 
    log_group = f"/aws/lambda/{function_name}"

    try:
        log_response = logs_client.filter_log_events(
            logGroupName=log_group,
            limit=1,  
            startTime=0,  
            interleaved=True, 
            orderBy='LogStreamName', 
        )

        if log_response['events']:
            last_invocation_time = log_response['events'][0]['timestamp']
            return last_invocation_time
        else:
            return "No recent invocations"

    except Exception as e:
        print(f"Error getting last run time for {function_name}: {e}")
        return str(e)
© www.soinside.com 2019 - 2024. All rights reserved.