def get_lambda_handler_extractor()

in codeguru_profiler_agent/aws_lambda/lambda_handler.py [0:0]


def get_lambda_handler_extractor():
    """
    This loads and returns a function from lambda or RIC source code that is able to load the customer's
    handler function.
    WARNING !! This is a bit dangerous since we are calling internal functions from other modules that we do not
    officially depend on. The idea is that this code should run only in a lambda function environment where we can know
    what is available. However if lambda developers decide to change their internal code it could impact this !
    """
    # First try to load the lambda RIC if it is available (i.e. python 3.9)
    # See https://github.com/aws/aws-lambda-python-runtime-interface-client
    ric_bootstrap_module = _try_to_load_module("awslambdaric.bootstrap")
    if ric_bootstrap_module is not None and hasattr(ric_bootstrap_module, '_get_handler'):
        return ric_bootstrap_module._get_handler

    # If no RIC module is available there should be a bootstrap module available
    # do not catch ModuleNotFoundError exceptions here as we cannot do anything if this fails.
    bootstrap_module = importlib.import_module("bootstrap")
    if hasattr(bootstrap_module, '_get_handler'):
        return bootstrap_module._get_handler
    else:
        return lambda handler_name: _python36_extractor(bootstrap_module, handler_name)