Simple Things

I forget things, simple things. I like to document such simple things, which leaves more space in my brain for un-important stuff my wife wants me to remember.

Python and CloudWatch

logging is the de-facto package for simple and complex python logging. For quick debugging, I sometimes use print statements. Who doesn’t? I wanted to understand how does AWS CloudWatch these two things. Hence, this experiment.

Here’s a simple python function that prints the environment variables available to it and the event object passed it was passed.

import logging
import os

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

def lambda_handler(event, context):
    print('## ENVIRONMENT VARIABLES')
    print(os.environ)
    print('## EVENT')
    print(event)
    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
    logger.info('## EVENT')
    logger.info(event)

Here’s the result in CloudWatch.

There’s no difference from what you would see if you ran this code in your terminal.

Logging Template

This is the template I use to log my python serverless functions in AWS Lambda.

import logging

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

def lambda_handler(event, context):
    logger.info('Logging from Lambda function..')