{"id":14450,"library":"aws-lambda-context","title":"AWS Lambda Context","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://pypi.org/project/aws-lambda-context/","tags":["aws","lambda","context","type-checking","testing","serverless"],"install":[{"cmd":"pip install aws-lambda-context","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required runtime environment.","package":"python","optional":false}],"imports":[{"symbol":"LambdaContext","correct":"from aws_lambda_context import LambdaContext"}],"quickstart":{"code":"from typing import Any\nfrom aws_lambda_context import LambdaContext\n\ndef lambda_handler(event: Any, context: LambdaContext) -> dict:\n    \"\"\"An example Lambda handler using LambdaContext for type hinting.\"\"\"\n    print(f\"Function Name: {context.function_name}\")\n    print(f\"Request ID: {context.aws_request_id}\")\n    print(f\"Memory Limit: {context.memory_limit_in_mb} MB\")\n    print(f\"Time remaining: {context.get_remaining_time_in_millis()} ms\")\n\n    # Example of creating a mock context for local testing\n    # In a real test, you'd likely use a mocking library like unittest.mock\n    mock_context = LambdaContext(\n        function_name='my-mock-function',\n        function_version='$LATEST',\n        invoked_function_arn='arn:aws:lambda:us-east-1:123456789012:function:my-mock-function',\n        memory_limit_in_mb=128,\n        aws_request_id='test-request-id-123',\n        log_group_name='/aws/lambda/my-mock-function',\n        log_stream_name='2026/04/16/[LATEST]abcdef123456',\n        identity=None,\n        client_context=None\n    )\n\n    # For local execution or testing, you might manually call:\n    # lambda_handler({}, mock_context)\n\n    return {\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}\n","lang":"python","description":"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."},"warnings":[{"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.","message":"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]","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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]","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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]","severity":"deprecated","affected_versions":"All versions (due to potential lack of future updates)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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.","cause":"The `aws-lambda-context` package or its dependencies were not correctly included in the AWS Lambda deployment package or layer.","error":"ModuleNotFoundError: No module named 'aws_lambda_context'"},{"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.","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.","error":"Mypy/Pylint/IDE reports 'LambdaContext' object has no attribute 'some_property'"},{"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)`.","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.","error":"Tests involving `context.get_remaining_time_in_millis()` return unexpected or static values."}],"ecosystem":"pypi"}