Registry / aws / aws-lambda-context

aws-lambda-context

JSON →
library1.1.0pypiunverified

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.

pip install aws-lambda-context
INSTALL
IMPORT
SIG · AWS-LAMBDA-CONTEXT
A
aws-lambda-context
awsenv1.1.0
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LambdaContext
from aws_lambda_context import LambdaContext

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!"}
Debug
Known issues
gotchaThis 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]
fix
For comprehensive local testing of time-sensitive logic or other dynamic runtime aspects, use a mocking library (e.g., `unittest.mock`) to control the behavior of `LambdaContext` methods and properties.
affects: All versions
gotchaThe `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]
fix
Evaluate your project's needs. If basic type hinting is sufficient, `aws-lambda-context` is fine. For broader serverless development best practices and tools, `pip install aws-lambda-powertools` and refer to its documentation.
affects: All versions
deprecatedThe 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]
fix
While the library functions, be aware that official support or future updates are uncertain. Consider migrating to actively maintained AWS-endorsed libraries like `Powertools for AWS Lambda (Python)` for long-term projects.
affects: All versions (due to potential lack of future updates)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'aws_lambda_context'
The `aws-lambda-context` package or its dependencies were not correctly included in the AWS Lambda deployment package or layer.
fix
Ensure 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'
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.
fix
Verify 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.
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.
fix
When 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)`.
Upgrade
Version history
1.1.0latest on PyPI · released Jul 17, 2019
Audit
Dependencies
pythonrequiredRequired runtime environment.
Agent activity
55 hits · last 30 days
node
48
OpenAI (training)
1
Resources

No resource links recorded.

aws-lambda-context — pip install aws-lambda-context · libregistry