Search code examples
pythonamazon-web-servicesboto3

How do I get the log group associated with a Lambda function in boto3?


I see that I can get the log group as an environment variable from within the Lambda itself through the environment variables but if I have a Cloudwatch Event of a Lambda success or failure, how can I use the Lambda function name to identify the log group it's writing to? I don't see any lambda or cloudwatch boto3 client methods that provide this functionality.

This provides some details about the Lambda function (including env variables) but doesn't include log group

client = boto3.client('lambda')
client.get_function(FunctionName=function_name)

There's this on the cloudwatch side, but it requires a prefix to the log group, no way to tie a resource ARN to the log group ARN.

client = boto3.client('cloudwatch')
client.describe_log_groups()

Solution

  • I have just explored that AWS have recently added the logging config details to get_function response. So now you can get log group name for a lambda like this:

    def get_log_group(function_arn):
        function_details = boto3.client('lambda').get_function(FunctionName=function_arn)
        return function_details.get('Configuration').get('LoggingConfig').get('LogGroup')
    

    it was missing when using boto3==1.26.161, but see it started being returned after updating to boto3==1.29.5, so make sure to check if you got some recent version if you don't see LoggingConfig in the response.