Last active
September 16, 2022 19:50
-
-
Save rswift/104758a00ed978b6638882fb9274815f to your computer and use it in GitHub Desktop.
AWS Lambda function to log salient details then exit...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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