Skip to content

Instantly share code, notes, and snippets.

@rswift
Last active September 16, 2022 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rswift/104758a00ed978b6638882fb9274815f to your computer and use it in GitHub Desktop.
Save rswift/104758a00ed978b6638882fb9274815f to your computer and use it in GitHub Desktop.
AWS Lambda function to log salient details then exit...
import logging
import os
log_level = os.environ['LOG_LEVEL'] if 'LOG_LEVEL' in os.environ else 'INFO'
logging_levels = {"DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL} # https://docs.python.org/3/library/logging.html#levels
logging_level = logging_levels[log_level] if log_level in logging_levels else logging.INFO
logger = logging.getLogger()
logger.setLevel(logging_level)
# if the function uses boto3
##for package in ['boto3', 'botocore', 'urllib3']:
## logging.getLogger(package).setLevel(logging.CRITICAL)
def lambda_handler(event, context):
logger.info(f"{context.function_name} ({context.function_version}) with event: {event}") # https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment