简体   繁体   中英

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. 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.

Event Rule: - 在此处输入图像描述

Is it possible to create 1 cloudwatch log for 1 Lambda Trigger??

Rachit,

Your Lambda function comes with a CloudWatch Logs log group, with a log stream for each instance of your function. The runtime sends details about each invocation to the log stream, and relays logs and other output from your function's code.

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. In case of Lambda, it's one stream per Lambda container where each container might process multiple events.

A log stream is a sequence of log events that share the same source. Each separate source of logs into CloudWatch Logs makes up a separate log stream.

Ref:

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 . 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.

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 . These resources take time to provision and this results in increased latency for the execution of the 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. This is known as keeping the function "warm". 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.

Notwithstanding this, it's worth pointing out that AWS does not keep the container running indefinitely. 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. 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.

You can read more about Lambda's execution context here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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