Specify log group for an AWS lambda?

前端 未结 4 1547
星月不相逢
星月不相逢 2020-12-05 06:27

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 conve

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 07:11

    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/
    

    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.

提交回复
热议问题