Capturing Lambda Logs In JSON Format
Reference: https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/
Recently AWS announced native support for capturing Lambda logs in JSON format. So let’s see how that works.
Step 1: Create a Python 3.10 Lambda function with following code:
import json
import logging
logger = logging.getLogger()
def lambda_handler(event, context):
logger.info("The API call ended successfully")
logger.error("The API call did not end successfully")
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Step 2: Go to Lambda configuration->Monitoring and operations tools and click on Edit
and set Application log level to INFO and System log level to WARN.
Step 3: Now execute the lambda function and check the logs in CloudWatch
If you change the log format back to text, you’ll see CloudWatch logs like this