Skip to content

Models

Common data models used across Lambda handlers.


Data models for Lambda handler configuration and execution.

Provides models for Lambda context, handler requests, and serialization utilities.

DefaultLambdaContext dataclass

DefaultLambdaContext(
    _function_name=(
        lambda: get_env_var(AWS_LAMBDA_FUNCTION_NAME_KEY)
        or DEFAULT_AWS_LAMBDA_FUNCTION_NAME
    )(),
    _function_version=(
        lambda: get_env_var(
            AWS_LAMBDA_FUNCTION_VERSION_KEY,
            default_value="1.0",
        )
    )(),
    _invoked_function_arn=(
        lambda: get_env_var(
            AWS_LAMBDA_FUNCTION_ARN_KEY, default_value=""
        )
    )(),
    _memory_limit_in_mb=(
        lambda: int(
            get_env_var(
                AWS_LAMBDA_FUNCTION_MEMORY_SIZE_KEY,
                default_value="1024",
            )
        )
    )(),
    _aws_request_id=(
        lambda: get_env_var(
            AWS_LAMBDA_FUNCTION_REQUEST_ID_KEY,
            default_value="",
        )
    )(),
    _log_group_name=(
        lambda: get_env_var(
            AWS_LAMBDA_LOG_GROUP_NAME_KEY, default_value=""
        )
    )(),
    _log_stream_name=(
        lambda: get_env_var(
            AWS_LAMBDA_LOG_STREAM_NAME_KEY, default_value=""
        )
    )(),
    _identity=(lambda: LambdaCognitoIdentity())(),
    _client_context=(lambda: LambdaClientContext())(),
)

Bases: LambdaContext

Standard implementation of LambdaContext for non-Lambda environments.

Provides a mock Lambda context for running handlers outside of AWS Lambda, such as in Docker containers or for local testing. All fields can be configured via environment variables.

LambdaHandlerRequest dataclass

LambdaHandlerRequest(
    handler=custom_field(
        mm_field=(
            mm.fields.Function(
                lambda obj: serialize_handler(obj.handler),
                deserialize_handler,
            )
        )
    ),
    event=custom_field(mm_field=(DictField())),
)

Bases: SchemaModel

Request model for dynamic Lambda handler invocation.

Contains the handler reference and event payload for routing Lambda invocations to the appropriate handler.

Attributes:

Name Type Description
handler LambdaHandlerType

The Lambda handler function to invoke.

event LambdaEvent

The event payload to pass to the handler.

deserialize_handler

deserialize_handler(handler)

Deserialize a handler from its qualified name.

Loads a handler from its fully qualified module path. Supports both function handlers and LambdaHandler subclasses.

Parameters:

Name Type Description Default
handler str

The fully qualified handler path (e.g., 'module.submodule.HandlerClass').

required

Returns:

Type Description
LambdaHandlerType

The Lambda handler function ready to be invoked.

Raises:

Type Description
ValueError

If the handler cannot be deserialized or is not a valid handler type.

Example
handler = deserialize_handler('my_module.MyHandler')
response = handler(event, context)
Source code in src/aibs_informatics_aws_lambda/common/models.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def deserialize_handler(handler: str) -> LambdaHandlerType:
    """Deserialize a handler from its qualified name.

    Loads a handler from its fully qualified module path. Supports both
    function handlers and LambdaHandler subclasses.

    Args:
        handler (str): The fully qualified handler path (e.g., 'module.submodule.HandlerClass').

    Returns:
        The Lambda handler function ready to be invoked.

    Raises:
        ValueError: If the handler cannot be deserialized or is not a valid handler type.

    Example:
        ```python
        handler = deserialize_handler('my_module.MyHandler')
        response = handler(event, context)
        ```
    """
    handler_components = handler.split(".")

    handler_module = as_module_type(".".join(handler_components[:-1]))
    handler_name = handler_components[-1]

    handler_code = getattr(handler_module, handler_name)
    if inspect.isfunction(handler_code):
        return cast(LambdaHandlerType, handler_code)
    elif inspect.isclass(handler_code) and issubclass(handler_code, LambdaHandler):
        logger.debug(f"Handler code is a class: {handler_code}. Calling `get_handler`...")
        return handler_code.get_handler()
    else:
        raise ValueError(
            f"Unable to deserialize handler: {handler}. "
            "It is not a function or a subclass of LambdaHandler."
        )

serialize_handler

serialize_handler(handler)

Serialize a Lambda handler to its qualified name.

Parameters:

Name Type Description Default
handler LambdaHandlerType

The Lambda handler function or class.

required

Returns:

Type Description
str

The fully qualified module path of the handler.

Source code in src/aibs_informatics_aws_lambda/common/models.py
90
91
92
93
94
95
96
97
98
99
def serialize_handler(handler: LambdaHandlerType) -> str:
    """Serialize a Lambda handler to its qualified name.

    Args:
        handler (LambdaHandlerType): The Lambda handler function or class.

    Returns:
        The fully qualified module path of the handler.
    """
    return get_qualified_name(handler)