简体   繁体   English

多个触发的 AWS Lambda 日志显示在单个 Cloudwatch 日志流中

[英]Multiple Triggered AWS Lambda Logs are displayed in a single Cloudwatch Log Streams

We have created a Lambda Function, which has to trigger after every minute.我们创建了一个 Lambda Function,它必须每分钟触发一次。 It is working as expected and showing the correct result.它按预期工作并显示正确的结果。 But logs stream, which are getting through Cloudwatch events, contains multiple Lambda trigger logs in a single Cloudwatch Log stream.但是通过 Cloudwatch 事件获取的日志 stream 在单个 Cloudwatch 日志 stream 中包含多个 Lambda 触发日志。

Event Rule: -活动规则:- 在此处输入图像描述

Is it possible to create 1 cloudwatch log for 1 Lambda Trigger??是否可以为 1 个 Lambda 触发器创建 1 个 cloudwatch 日志?

Rachit,拉希特,

Your Lambda function comes with a CloudWatch Logs log group, with a log stream for each instance of your function.您的 Lambda function 附带一个 CloudWatch Logs 日志组,其中包含一个日志 stream 用于您的 ZC1C4FAB45264C1783 的每个实例。 The runtime sends details about each invocation to the log stream, and relays logs and other output from your function's code.运行时将有关每个调用的详细信息发送到日志 stream,并从您的函数代码中中继日志和其他 output。

Moreover, From the AWS Cloudwatch documentation you can see that a log stream is created each time the logs come from a different event source.此外,从AWS Cloudwatch 文档中,您可以看到每次日志来自不同的事件源时都会创建日志 stream。 In case of Lambda, it's one stream per Lambda container where each container might process multiple events.对于 Lambda,它是每个 Lambda 容器一个 stream,其中每个容器可能处理多个事件。

A log stream is a sequence of log events that share the same source.日志 stream 是共享同一源的日志事件序列。 Each separate source of logs into CloudWatch Logs makes up a separate log stream. CloudWatch Logs 中的每个单独的日志源构成一个单独的日志 stream。

Ref:参考:

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html

As per the AWS Lambda documentation here , a log stream represents an instance of your function .根据此处的 AWS Lambda 文档,日志 stream 表示您的 function 的实例 In other words, a log stream represents the logs from a single execution environment for your Lambda Function... The execution environment is also called the context (one of the arguments you can pass in to your handler) and the reason you're not getting a new log stream with each invocation is because of the context in which Lambda functions execute. In other words, a log stream represents the logs from a single execution environment for your Lambda Function... The execution environment is also called the context (one of the arguments you can pass in to your handler) and the reason you're not每次调用都获得一个新日志 stream 是因为 Lambda 函数执行的上下文

When you invoke your Lambda function, AWS loads the container that holds your function code and provisions the requested resources that are required to enable your function to execute: CPU, memory, networking etc. These all form the functions execution environment which is also called the context . When you invoke your Lambda function, AWS loads the container that holds your function code and provisions the requested resources that are required to enable your function to execute: CPU, memory, networking etc. These all form the functions execution environment which is also called the上下文 These resources take time to provision and this results in increased latency for the execution of the function.这些资源需要时间来配置,这会导致 function 的执行延迟增加。 This is commonly known as a "cold start".这通常称为“冷启动”。

In order to mitigate this undesired latency, or cold start, with each invocation, after your function has completed its initial execution, instead of terminating the execution environment, AWS keeps the container and execution environment running and the resources such as cpu, memory and networking, provisioned and ready for and in anticipation of, the next invocation.为了在每次调用时缓解这种不希望出现的延迟或冷启动,在您的 function 完成其初始执行后,AWS 不会终止执行环境,而是保持容器和执行环境的运行以及 cpu、memory 和网络等资源的运行, 为下一次调用提供并准备好并期待下一次调用。 This is known as keeping the function "warm".这被称为保持 function“温暖”。 When the container is warm, subsequent invocations of your function are executed in the same execution environment, or context , as the previous invocation, and because the invocation was executed by the same instance of the function the logs are written to the same log stream as the previous invocation(s), which is the log stream that represents that instance / execution environment / context of the function. When the container is warm, subsequent invocations of your function are executed in the same execution environment, or context , as the previous invocation, and because the invocation was executed by the same instance of the function the logs are written to the same log stream as先前的调用,即日志 stream,表示 function 的实例/执行环境/上下文。

Notwithstanding this, it's worth pointing out that AWS does not keep the container running indefinitely.尽管如此,值得指出的是,AWS 不会让容器无限期地运行。 If there is no subsequent invocation within a given period of time (there's no exact period of time but it's generally considered to be between 30 and 45 minutes, source ) AWS will terminate the container, and release the resources for use by another function.如果在给定时间段内没有后续调用(没有确切的时间段,但通常认为在 30 到 45 分钟之间, 来源)AWS 将终止容器,并释放资源供另一个 function 使用。 The next time the Lambda function is invoked, AWS will repeat the provisioning process for the function and a new execution environment will be created, and this will cause your functions logs to be written to a new log stream which represents the new execution environment / context / instance of your function. The next time the Lambda function is invoked, AWS will repeat the provisioning process for the function and a new execution environment will be created, and this will cause your functions logs to be written to a new log stream which represents the new execution environment / context / 你的 function 的实例。

You can read more about Lambda's execution context here .您可以在此处阅读有关 Lambda 执行上下文的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM