Specify log group for an AWS lambda?
I find that @lingrlongr's answer is partially correct.
First, to answer the original question, you cannot specify a custom log group name for the lambda function to write to.
The lambda log group name always follows this pattern:
/aws/lambda/<function-name>
The lambda function will first check if a log group exists with this name.
- If it exists, then it will use that.
- If it does not exist, it will create a log group with that pattern.
Hence, if you want to add settings, such as RetentionInDays
and SubscriptionFilter
, make sure your CloudFormation or SAM template creates the LogGroup
before the lambda function. If your lambda function is created first, an error will be thrown when creating the LogGroup saying that the LogGroup already exists. So, the lambda function should have DependsOn: LogGroup
instead of the other way round.
Also, make sure you are not using Ref
or GetAtt
to reference the lambda function inside the LogGroup because that creates an implicit dependency on the lambda function causing the lambda function being created before the LogGroup.
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.
"MyLambdaFunctionLogGroup": {
"Type": "AWS::Logs::LogGroup",
"DependsOn": "MyLambdaFunction",
"DeletionPolicy": "Retain",
"Properties": {
"LogGroupName": {"Fn::Join": ["", ["/aws/lambda/", {"Ref": "MyLambdaFunction"}]]},
"RetentionInDays": 14
}
}
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?
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...