简体   繁体   English

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

[英]Which functions should I use to read aws lambda log

Once my lambda run is finished, I am getting this payload as a result:一旦我的 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"
  } 
}

I would like to read logs of my run from cloudwatch and also memory usage which I can see in lambda monitoring tab:我想从 cloudwatch 读取我的运行日志以及我可以在 lambda 监控选项卡中看到的内存使用情况: 在此处输入图片说明

How can do it via sdk?如何通过 sdk 做到这一点? Which functions should I use?我应该使用哪些功能?

I am using nodejs.我正在使用 nodejs。

You need to discover the log stream name that has been assigned to the Lambda function invocation.您需要发现已分配给 Lambda 函数调用的日志流名称。 This is available inside the Lambda function's context .这在 Lambda 函数的context可用。

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

Results in the following log:结果在以下日志中:

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] }

So, the CloudWatch Logs stream name is available in context.logStreamName .因此,CloudWatch Logs 流名称在context.logStreamName可用。 I'm not aware of an API to map a Lambda request ID to a log stream name after the fact, so you may need to return this or somehow persist the mapping.我不知道有什么 API 可以在事后将 Lambda 请求 ID 映射到日志流名称,因此您可能需要返回它或以某种方式保留映射。

Finding logs of a specific request-id can be done via AWS cloudwatch API.可以通过 AWS cloudwatch API 查找特定请求 ID 的日志。

You can use [filterLogEvents][1] API to extract (using regex) the relevant START and REPORT logs to gather the relevant information of the memory usage (You will also get the log stream name in the response for future use).您可以使用 [filterLogEvents][1] API 提取(使用正则表达式)相关的 START 和 REPORT 日志以收集内存使用的相关信息(您还将在响应中获得日志流名称以备将来使用)。

If you want to gather all the relevant logs of a specific invocation you will need to query create pairs of START and REPORT logs and query for all the logs in the specific timeframe between them on a specific log stream.如果要收集特定调用的所有相关日志,则需要查询创建 START 和 REPORT 日志对,并查询特定日志流上特定时间范围内的所有日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该将 AWS SAM 用于“简单”的 Lambda 函数吗? - Should I use AWS SAM for "simple" Lambda functions? 我应该使用哪种 AWS API Gateway 授权方类型来通过 Okta 保护我的 API? 拉姆达/认知? - Which AWS API Gateway Authorizer Type should I use to protect my APIs with Okta? Lambda/Cognito? AWS Lambda 日志 - 读取为字符串 - AWS Lambda log - read as a string 是否应为不同的AWS lambda函数创建不同的git存储库? - Should I create different git repository for different AWS lambda functions? 我应该使用哪个 aws 数据库? - Which aws database should I use? 如何在AWS Lambda函数中使用异步库调用? - How do I use async library calls in AWS Lambda functions? 我应该使用“添加目的地”从 Lambda 发送 AWS SQS 吗? - AWS SQS Send From Lambda should I use "Add Destination"? 如何将 chrome-aws-lambda 模块与 AWS lambda 函数一起使用? - How to use chrome-aws-lambda module with AWS lambda functions? 当仅由 Lambda 功能使用时,我是否应该将 AWS Elasticsearch 域放入 VPC - Should I put AWS Elasticsearch domain in VPC when it's consumed by Lambda functions only 我应该使用哪种AWS服务来处理大型文本文件? - Which AWS service should I use to process large text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM