AWS Lambda Context

1.1.0 · active · verified Thu Apr 16

aws-lambda-context is a micro-library, currently at version 1.1.0, that provides the `LambdaContext` class for Python applications. Its primary purpose is to enable type checking and facilitate local testing of AWS Lambda functions by providing a well-defined interface for the context object, mimicking the structure of the context object passed by the AWS Lambda runtime. It aims to help developers ensure their code adheres to the Lambda context interface during development and testing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `LambdaContext` for type hinting in a standard AWS Lambda handler function. It also shows how to instantiate a `LambdaContext` object for local development and testing, mimicking the properties provided by the actual AWS Lambda runtime.

from typing import Any
from aws_lambda_context import LambdaContext

def lambda_handler(event: Any, context: LambdaContext) -> dict:
    """An example Lambda handler using LambdaContext for type hinting."""
    print(f"Function Name: {context.function_name}")
    print(f"Request ID: {context.aws_request_id}")
    print(f"Memory Limit: {context.memory_limit_in_mb} MB")
    print(f"Time remaining: {context.get_remaining_time_in_millis()} ms")

    # Example of creating a mock context for local testing
    # In a real test, you'd likely use a mocking library like unittest.mock
    mock_context = LambdaContext(
        function_name='my-mock-function',
        function_version='$LATEST',
        invoked_function_arn='arn:aws:lambda:us-east-1:123456789012:function:my-mock-function',
        memory_limit_in_mb=128,
        aws_request_id='test-request-id-123',
        log_group_name='/aws/lambda/my-mock-function',
        log_stream_name='2026/04/16/[LATEST]abcdef123456',
        identity=None,
        client_context=None
    )

    # For local execution or testing, you might manually call:
    # lambda_handler({}, mock_context)

    return {"statusCode": 200, "body": "Hello from Lambda!"}

view raw JSON →