我应该使用哪些函数来读取 aws lambda 日志

Which functions should I use to read aws lambda log

我的 lambda 运行 完成后,我得到了这个有效负载:

{
  "version": "1.0",
  "timestamp": "2020-09-30T19:20:03.360Z",
  "requestContext": {
    "requestId": "2de65baf-f630-48a7-881a-ce3145f1127d",
    "functionArn": "arn:aws:lambda:us-east-2:044739556748:function:puppeteer:$LATEST",
    "condition": "Success",
    "approximateInvokeCount": 1
  }, 
  "responseContext": {
    "statusCode": 200,
    "executedVersion": "$LATEST"
  } 
}

我想从 cloudwatch 中读取 运行 的日志以及我可以在 lambda 监控选项卡中看到的内存使用情况:

如何通过sdk实现?我应该使用哪些功能?

我正在使用 nodejs。

您需要发现分配给 Lambda 函数调用的日志流名称。这在 Lambda 函数的 context.

中可用
exports.handler = async (event, context) => {
  console.log('context', context);
};

结果如下:

context { callbackWaitsForEmptyEventLoop: [Getter/Setter],
  succeed: [Function],
  fail: [Function],
  done: [Function],
  functionVersion: '$LATEST',
  functionName: 'test-log',
  memoryLimitInMB: '128',
  logGroupName: '/aws/lambda/test-log',
  logStreamName: '2020/10/03/[$LATEST]f123a3c1bca123df8c12e7c12c8fe13e',
  clientContext: undefined,
  identity: undefined,
  invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456781234:function:test-log',
  awsRequestId: 'e1234567-6b7c-4477-ac3d-74bc62b97bb2',
  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis] }

因此,CloudWatch Logs 流名称在 context.logStreamName 中可用。我不知道事后 API 将 Lambda 请求 ID 映射到日志流名称,因此您可能需要 return 这个或以某种方式保留映射。

可以通过 AWS cloudwatch API.

查找特定 request-id 的日志

您可以使用 [filterLogEvents][1] API 提取(使用正则表达式)相关的 START 和 REPORT 日志以收集内存使用的相关信息(您还将在供将来使用的响应)。

如果您想收集特定调用的所有相关日志,您将需要查询创建成对的 START 和 REPORT 日志,并在特定日志流上查询它们之间特定时间范围内的所有日志。