如何从 AWS CLI 打印某个 Lambda 函数的最新 Cloudwatch 日志?

How to print the most recent Cloudwatch log for a certain Lambda function from the AWS CLI?

我正在尝试使用 AWS CLI 和 ASK CLI 编写 Alexa 技能代码,我希望能够使用 Alexa 模拟器并直接从命令行查看控制台日志以简化操作,但是我不确定如何从命令行查看最后一个。

我已经安装了 AWS 和 ASK CLI 并且能够查看 Cloudwatch 日志,但是没有快速查看最后一个日志的方法。

有几个有用的开源工具可以提供帮助:

您可以使用aws logs describe-log-streams to get the latest stream name and then aws logs get-log-events自己获取日志记录。

LOG_GROUP=log-group
LOG_STREAM=`aws logs describe-log-streams --log-group-name $LOG_GROUP --max-items 1 --order-by LastEventTime --descending --query logStreams[].logStreamName --output text | head -n 1`
aws logs get-log-events --log-group-name $LOG_GROUP --log-stream-name $LOG_STREAM --query events[].message --output text

使用最新的 AWS CLI,您还可以使用 tail。

aws logs tail $LOG_GROUP --follow

使用 log-type 标志,您将能够从执行中获取 cloudwatch 日志。例如:

LOG_RESULT=$(aws lambda invoke --function-name arn:aws:lambda:REGION:111122223333:function:YOUR_LAMBDA_NAME --log-type Tail outfile --query LogResult --output text)

echo $LOG_RESULT | base64 -D

输出:

START RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59 Version: $LATEST
END RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59
REPORT RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59  Duration: 71.48 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 74 MB

这假定函数是同步执行的,但是 eventType RequestResponse

如果您仍然需要那里的实际 CloudWatch 日志,您可以根据请求 ID 进行查询。

REQUEST_ID=$(echo $LOG_RESULT | base64 -D | grep START | cut -d " " -f 3)
aws logs filter-log-events --log-group-name /aws/lambda/YOUR_LAMBDA_NAME --filter-pattern \"$REQUEST_ID\" 

输出:

{
    "events": [
        {
            "logStreamName": "2019/06/29/[$LATEST]f94a9c338ec445cda688c015b460621d",
            "timestamp": 1561775888037,
            "message": "START RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59 Version: $LATEST\n",
            "ingestionTime": 1561775888119,
            "eventId": "34828766136322027826299000340819150179641895561445048320"
        },
        {
            "logStreamName": "2019/06/29/[$LATEST]f94a9c338ec445cda688c015b460621d",
            "timestamp": 1561775888113,
            "message": "END RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59\n",
            "ingestionTime": 1561775903178,
            "eventId": "34828766138016884461387327717780753707358087734557278208"
        },
        {
            "logStreamName": "2019/06/29/[$LATEST]f94a9c338ec445cda688c015b460621d",
            "timestamp": 1561775888113,
            "message": "REPORT RequestId: 3ab54034-ed40-4fd7-b660-17db96a25f59\tDuration: 71.48 ms\tBilled Duration: 100 ms \tMemory Size: 128 MB\tMax Memory Used: 74 MB\t\n",
            "ingestionTime": 1561775903178,
            "eventId": "34828766138016884461387327717780753707358087734557278209"
        }
    ],
    "searchedLogStreams": [
        {
            "logStreamName": "2019/06/29/[$LATEST]f94a9c338ec445cda688c015b460621d",
            "searchedCompletely": true
        }
    ]
}

但是,此输出顺序不正确,包括有关日志流本身以及搜索了哪些日志组的信息。

您可以使用此命令对其进行过滤:

aws logs filter-log-events --log-group-name /aws/lambda/YOUR_LAMBDA_NAME --filter-pattern \"$REQUEST_ID\" --query 'sort_by(events, &timestamp)[*].[message]' | jq .[][0]

输出:

"START RequestId: 610dcebf-bb7b-4c39-895b-8989b46386a8 Version: $LATEST\n"
"END RequestId: 610dcebf-bb7b-4c39-895b-8989b46386a8\n"
"REPORT RequestId: 610dcebf-bb7b-4c39-895b-8989b46386a8\tDuration: 154.04 ms\tBilled Duration: 200 ms \tMemory Size: 128 MB\tMax Memory Used: 73 MB\t\n"

如果有人还在苦苦挣扎,想要使用 AWS CLI 提取最新的日志流

aws logs describe-log-streams --log-group-name '/aws/lambda/ [YOUR_LAMBDA_FUNCTION_NAME_GOES_HERE]' --query 'logStreams[*].logStreamName' --max-items 1 --order-by LastEventTime --descending

或者您想查询您可以使用的单个 lambda 的所有日志流

aws logs describe-log-streams --log-group-name '/aws/lambda/[YOUR_LAMBDA_FUNCTION_NAME_GOES_HERE]' --query 'logStreams[*].logStreamName'