Type annotations for boto3 Lambda
Type annotations for boto3 Lambda 1.42.85 service generated with mypy-boto3-builder 8.12.0. This library provides static type checking for AWS Boto3's Lambda client, improving code quality, autocomplete, and error detection in IDEs and with type checkers like Mypy and Pyright. It is part of the `types-boto3` ecosystem, which aims to provide comprehensive type stubs for all Boto3 services. The library typically releases new versions in sync with `boto3` releases.
Warnings
- breaking Support for Python 3.8 has been removed in `mypy-boto3-builder` version 8.12.0 and consequently for all generated `types-boto3` packages, including `types-boto3-lambda`.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` version 8.9.0. TypeDefs for packed method arguments now use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`).
- gotcha When using `boto3-stubs-lite[lambda]` or `mypy-boto3-lambda` (standalone package), explicit type annotations are often required for `boto3.client` calls to enable full type inference. Without them, IDEs and type checkers might not provide complete suggestions or checks.
- gotcha PyCharm can experience performance issues with `Literal` overloads in `types-boto3` packages, leading to high CPU usage.
- gotcha To avoid `types-boto3-lambda` being a runtime dependency in production code, it is recommended to wrap type imports within a `typing.TYPE_CHECKING` block.
Install
-
pip install 'boto3-stubs[lambda]' -
pip install mypy-boto3-lambda
Imports
- LambdaClient
from mypy_boto3_lambda.client import LambdaClient
- InvokeFunctionResponseTypeDef
from mypy_boto3_lambda.type_defs import InvokeFunctionResponseTypeDef
- RuntimeType
from mypy_boto3_lambda.literals import RuntimeType
Quickstart
import boto3
import os
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_lambda.client import LambdaClient
from mypy_boto3_lambda.type_defs import InvokeFunctionResponseTypeDef
def get_lambda_client() -> "LambdaClient":
"""Returns a typed Lambda client."""
return boto3.client("lambda")
def invoke_my_lambda(client: "LambdaClient", function_name: str, payload: dict) -> "InvokeFunctionResponseTypeDef":
"""Invokes a Lambda function with type hints."""
response = client.invoke(
FunctionName=function_name,
InvocationType='RequestResponse',
Payload=bytes(str(payload).encode('utf-8'))
)
return response
# Example Usage
if __name__ == "__main__":
lambda_client = get_lambda_client()
# Use os.environ.get for dynamic configuration in runnable examples
function_name = os.environ.get('LAMBDA_FUNCTION_NAME', 'my-test-lambda-function')
example_payload = {"greeting": "hello from types-boto3-lambda"}
try:
print(f"Invoking Lambda function: {function_name}")
response = invoke_my_lambda(lambda_client, function_name, example_payload)
print(f"Lambda Invocation Status Code: {response.get('StatusCode')}")
# If the Lambda returns a JSON payload, you might want to read and decode it
# payload_stream = response.get('Payload')
# if payload_stream:
# payload_data = payload_stream.read().decode('utf-8')
# print(f"Lambda Response Payload: {payload_data}")
except Exception as e:
print(f"Error invoking Lambda: {e}")