AWS Lambda Context
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
-
ModuleNotFoundError: No module named 'aws_lambda_context'
cause The `aws-lambda-context` package or its dependencies were not correctly included in the AWS Lambda deployment package or layer.fixEnsure the package is installed in your Lambda environment. For deployment packages, use `pip install aws-lambda-context -t path/to/your/package/root`. For Lambda Layers, package the library correctly into a layer and attach it to your function. -
Mypy/Pylint/IDE reports 'LambdaContext' object has no attribute 'some_property'
cause Attempting to access a property on the `context` object that is not defined in the `LambdaContext` class, or is not an official AWS Lambda context property for Python.fixVerify the property name against the official AWS Lambda context object documentation for Python (e.g., `function_name`, `aws_request_id`, `memory_limit_in_mb`). If it's a custom property, you may need to subclass `LambdaContext` or use `typing.Dict` for `context` to bypass strict checking. -
Tests involving `context.get_remaining_time_in_millis()` return unexpected or static values.
cause The `LambdaContext` class provides a default implementation for dynamic methods like `get_remaining_time_in_millis()` which is static. It's designed for type compatibility, not runtime simulation.fixWhen writing unit tests, explicitly mock the `get_remaining_time_in_millis` method (and similar dynamic methods) of your `LambdaContext` instance to return controlled, predictable values that simulate the desired test scenarios. E.g., `mock_context.get_remaining_time_in_millis = MagicMock(return_value=10000)`.
Warnings
- gotcha This library provides an interface for type checking and local testing, but it does not fully emulate the AWS Lambda runtime environment's dynamic behaviors. For example, `context.get_remaining_time_in_millis()` will return a static mock value (e.g., 300000ms) unless explicitly mocked in your tests to reflect real-time countdown. [1, 2, 5]
- gotcha The `aws-lambda-context` library is a minimal type definition tool. For a richer set of utilities, including advanced logging, metrics, tracing, and more comprehensive event/context typing, consider using `Powertools for AWS Lambda (Python)`, which is actively maintained by AWS and includes its own context object definition. [5, 11]
- deprecated The official GitHub repository link provided on the PyPI page (`https://github.com/aws-lambda-context/aws-lambda-context`) is currently broken (404 Not Found). This suggests the project might be unmaintained or its source has moved without an update to PyPI. [2]
Install
-
pip install aws-lambda-context
Imports
- LambdaContext
from aws_lambda_context import LambdaContext
Quickstart
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!"}