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-contextVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
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.
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.
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)`.
No resource links recorded.