阅读背景:

为AWS lambda指定日志组?

来源:互联网 

Is there a way to specify the CloudWatch log group that an AWS lambda logs to? It seems to be generated directly from the lambda name; however, it would be especially convenient to, for example, aggregate multiple lambdas to a single log group. We are especially interested in specifying the log group when the lambda is created by a CloudFormation template.

有没有办法指定AWS lambda记录到的CloudWatch日志组?它似乎是直接从lambda名称生成的;但是,例如,将多个lambda聚合到单个日志组会特别方便。我们特别感兴趣的是在由CloudFormation模板创建lambda时指定日志组。

4 个解决方案

#1


12  

I don't think that is possible.

我不认为这是可能的。

Even if it were possible, each AWS Lambda instance would still write to its own log-stream. And though different invocations of the same lambda can write to the same log-stream (when the lambda instance is reused), this will definitely not be the case for different lambdas (since they must use different lambda instances).

即使可能,每个AWS Lambda实例仍将写入其自己的日志流。虽然同一个lambda的不同调用可以写入同一个日志流(当重用lambda实例时),但对于不同的lambda来说肯定不是这种情况(因为它们必须使用不同的lambda实例)。

As a result, you must have a tool that aggregates multiple log-stream. If so, what's the problem with making it a little more generic, so that it can aggregate log-streams from different log groups?

因此,您必须拥有一个聚合多个日志流的工具。如果是这样,那么使它更通用的问题是什么,以便它可以聚合来自不同日志组的日志流?

#2


14  

Actually, maybe you can, to an extent at least. I too was in search of the answer and gave this a try. Here's a snippet of two resources; the lambda function and the log group:

实际上,也许你至少可以达到一定程度。我也在寻找答案并尝试了一下。这是两个资源的片段; lambda函数和日志组:

"MyLambdaFunction": {
    "Type": "AWS::Lambda::Function",
    "DependsOn": "ReadWriteRole",
    "Properties": {
        //snip
    }
},

"MyLambdaFunctionLogGroup": {
    "Type": "AWS::Logs::LogGroup",
    "DependsOn": "MyLambdaFunction",
    "Properties": {
        "LogGroupName": {"Fn::Join": ["", ["/aws/lambda/", {"Ref": "MyLambdaFunction"}]]},
        "RetentionInDays": 14
    }
},

I found that the log group was created with a retention of 14 days as indicated. When the lambda function runs, it does create log streams in this group. When I deleted the stack, however, it seems that the log groups is not deleted, and the retention is now set to never expire. Perhaps that's good enough so the streams don't get too out of hand...

我发现创建的日志组保留了14天,如图所示。当lambda函数运行时,它会在此组中创建日志流。但是,当我删除堆栈时,似乎没有删除日志组,并且保留现在设置为永不过期。也许这就足够了所以流不会太失控......

#3


3  

Creating the log group as mentioned as one of the answers works. To keep the retention policy after the stack is deleted, just add a DeletionPolicy.

如上所述创建日志组作为答案之一。要在删除堆栈后保留保留策略,只需添加DeletionPolicy即可。

"MyLambdaFunctionLogGroup": {
  "Type": "AWS::Logs::LogGroup",
  "DependsOn": "MyLambdaFunction",
  "DeletionPolicy": "Retain",
  "Properties": {
    "LogGroupName": {"Fn::Join": ["", ["/aws/lambda/", {"Ref": "MyLambdaFunction"}]]},
    "RetentionInDays": 14
  }
}

#4


0  

Interesting. . . when I try this, the log group is created but no streams are being written to it. My Lambda continues to write to the default log stream. . . the Lambda's permissions should allow access to the new group.

有趣。 。 。当我尝试这个时,创建了日志组,但没有写入任何流。我的Lambda继续写入默认日志流。 。 。 Lambda的权限应该允许访问新组。


分享到: