Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify log group for an 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.

like image 748
JohnJ Avatar asked Aug 30 '16 15:08

JohnJ


People also ask

How do I create a log group in aws?

To create a log groupOpen the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Log groups. Choose Actions, and then choose Create log group. Enter a name for the log group, and then choose Create log group.

What is log group name in aws?

Log group names must be unique within a Region for an AWS account. Log group names can be between 1 and 512 characters long. Log group names consist of the following characters: a-z, A-Z, 0-9, '_' (underscore), '-' (hyphen), '/' (forward slash), and '. ' (period).


2 Answers

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:

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

like image 58
lingrlongr Avatar answered Oct 07 '22 10:10

lingrlongr


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

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?

like image 27
Leon Avatar answered Oct 07 '22 09:10

Leon