Amazon lambda does not show python logs

Question:

My API(Python) is deployed on Amazon Lambda. The problem is when I request my API I get the internal server error. I can tail the Lambda logs but I don’t see the actual error or stack trace where the code crashed. When I tail the logs I just get the following output.

START RequestId: 62341bgd-6231-11e8-8c5b-25793532a32u Version: $LATEST
END RequestId: 62341b0d-6231-1128-8r5b-2b793032a3ed
REPORT RequestId: 6234te0b-6rte-aaa8-au5a-21t93132r3rt  Duration: 0.46 ms

How can I see the actual stack trace of my python api for debugging?

Asked By: Hassan Abbas

||

Answers:

Lambda always tries to write the Python stack trace to CloudWatch. Make sure your function has the required permissions:

    {
        "Effect": "Allow",
        "Action": [
            "logs:CreateLogStream",
            "logs:PutLogEvents"
        ],
        "Resource": "arn:aws:logs:eu-west-1:123456789012:*"
    },
    {
        "Effect": "Allow",
        "Action": "logs:CreateLogGroup",
        "Resource": "*"
    }
Answered By: FelixEnescu

If you are using Lambda_basic_execution role, simple print in python will show logs in cloudwatch.

Answered By: singh30

Initial Investigation:

By default lambda function created log group in CloudWatch by the name of my API. For example, /aws/lambda/my_api_name which shows the output something like this.

START RequestId: 62341bgd-6231-11e8-8c5b-25793532a32u Version: $LATEST
END RequestId: 62341b0d-6231-1128-8r5b-2b793032a3ed
REPORT RequestId: 6234te0b-6rte-aaa8-au5a-21t93132r3rt  Duration: 0.46 ms

Problem Resolution:

I created a service role for my cloud watch(Default policy for all the rights for the cloudWatch already provided by AWS) and then put this role into my API. I followed this approach. However, another step could be combine the newly created execution logs with the default one so we have one log group to tail.

Answered By: Hassan Abbas

my logs were enabled but still it didnt printed anything, it also didnt show added log in CloudWatch. but when I commented a print statement which was trying to print too much data from dynamo db, it started working again so maybe somewhere else in your code you used print to print huge data so disable it

you cant print too much data or it will neither print anything in test nor will it add log in CloudWatch

Answered By: Naeem Malik

I see this is a bit old question. But just leaving a comment here, as I encountered the same problem yet using Python 3.9. And this how I solved the problem. When using Lambda, use:

logger = logging.getLogger()
logger.setLevel(logging.INFO)

It does not work when using:

logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)

Answered By: hlesnt395