AWS Lambda
Developer Guide

AWS Lambda Function Logging in Python

Your Lambda function comes with a CloudWatch Logs log group, with a log stream for each instance of your function. The runtime sends details about each invocation to the log stream, and relays logs and other output from your function's code.

To output logs from your function code, you can use the print method, or any logging library that writes to stdout or stderr. The following example logs the values of environment variables and the event object.

Example lambda_function.py

import json import os def lambda_handler(event, context): print('## ENVIRONMENT VARIABLES') print(os.environ) print('## EVENT') print(event) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }

For more detailed logs, use the logging library.

import json import os import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): logger.info('## ENVIRONMENT VARIABLES') logger.info(os.environ) logger.info('## EVENT') logger.info(event) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }

The output from logger includes the log level, timestamp, and request ID.

[INFO] 2019-04-21T23:24:14.135Z 00d3cdad-8aaf-42b2-af4e-6f8b2cae00a5 ## EVENT [INFO] 2019-04-21T23:24:14.135Z 00d3cdad-8aaf-42b2-af4e-6f8b2cae00a5 {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

The Lambda console shows log output when you test a function on the function configuration page. To view logs for all invocations, use the CloudWatch Logs console.

To view your Lambda function's logs

  1. Open the Logs page of the CloudWatch console.

  2. Choose the log group for your function (/aws/lambda/function-name).

  3. Choose the first stream in the list.

Each log stream corresponds to an instance of your function. New streams appear when you update your function and when additional instances are created to handle multiple concurrent invocations. To find logs for specific invocations, you can instrument your function with X-Ray and record details about the request and log stream in the trace. For a sample application that correlates logs and traces with X-Ray, see Error Processor Sample Application for AWS Lambda.

To get logs for an invocation from the command line, use the --log-type option. The response includes a LogResult field that contains up to 4 KB of base64-encoded logs from the invocation.

$ aws lambda invoke --function-name my-function out --log-type Tail { "StatusCode": 200, "LogResult": "U1RBUlQgUmVxdWVzdElkOiA4N2QwNDRiOC1mMTU0LTExZTgtOGNkYS0yOTc0YzVlNGZiMjEgVmVyc2lvb...", "ExecutedVersion": "$LATEST" }

You can use the base64 utility to decode the logs.

$ aws lambda invoke --function-name my-function out --log-type Tail \ --query 'LogResult' --output text | base64 -d START RequestId: 8e827ab1-f155-11e8-b06d-018ab046158d Version: $LATEST Processing event... END RequestId: 8e827ab1-f155-11e8-b06d-018ab046158d REPORT RequestId: 8e827ab1-f155-11e8-b06d-018ab046158d Duration: 29.40 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB

base64 is available on Linux, macOS, and Ubuntu on Windows. For macOS, the command is base64 -D.

Log groups are not deleted automatically when you delete a function. To avoid storing logs indefinitely, delete the log group, or configure a retention period after which logs are deleted automatically.