Hello!
In a previous article, I showed how to make AWS lambda functions log JSON objects to CloudWatch (⬅️ start there if you’re new to JSON logs). The pattern in that post had a flaw: it didn’t pass the aws_request_id
. I was writing small functions to glue together bits of deployment automation and I didn’t need it. It was easy to correlate logs just with timestamps. Not every case is that simple. Sometimes you need that ID. Fortunately, Paulie Pena IV gave me some tips on how to pass it through.
We can look up the aws_request_id
in the lambda function’s context
object. To add it to log events, the python-json-logger
library supports custom fields. To add a field for all log events, we need to subclass jsonlogger.JsonFormatter
. Then we can save the ID into the new field and resolve it in the format string. Here’s the code from before with that change:
import logging
from pythonjsonlogger import jsonlogger
class CustomJsonFormatter(jsonlogger.JsonFormatter):
def __init__(self, *args, **kwargs):
self.aws_request_id = kwargs.pop('aws_request_id')
super().__init__(*args, **kwargs)
def add_fields(self, log_record, record, message_dict):
super().add_fields(log_record, record, message_dict)
log_record['aws_request_id'] = self.aws_request_id
def setup_logging(log_level, aws_request_id):
logger = logging.getLogger()
# Testing showed lambda sets up one default handler. If there are more,
# something has changed and we want to fail so an operator can investigate.
assert len(logger.handlers) == 1
logger.setLevel(log_level)
json_handler = logging.StreamHandler()
formatter = CustomJsonFormatter(
fmt='%(aws_request_id)s %(asctime)s %(levelname)s %(name)s %(message)s',
aws_request_id=aws_request_id
)
json_handler.setFormatter(formatter)
logger.addHandler(json_handler)
logger.removeHandler(logger.handlers[0])
def lambda_handler(event, context):
setup_logging(logging.DEBUG, context.aws_request_id)
logger = logging.getLogger()
logger.info('Huzzah!')
Now our logs contain the aws_request_id
:

Hope that helps,
Adam
Need more than just this article? We’re available to consult.
You might also want to check out these related articles: