Type annotations for boto3 Lambda

1.42.85 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to obtain a type-hinted AWS Lambda client and invoke a function. It includes the necessary `TYPE_CHECKING` guard to ensure the type stubs are only used during static analysis, avoiding a runtime dependency. The function name is retrieved from an environment variable for reusability.

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}")

view raw JSON →