CloudWatchLogs Type Annotations for Boto3

1.42.83 · active · verified Fri Apr 10

mypy-boto3-logs provides static type annotations for the `boto3` CloudWatchLogs service, enhancing code quality and developer experience. It is generated by the `mypy-boto3-builder` project and is compatible with various type checkers like mypy and pyright, as well as IDEs like VSCode and PyCharm. The current version is 1.42.83, with frequent updates aligning with `boto3` releases and builder improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to obtain a type-hinted CloudWatchLogs client and use it with type-safe operations. The `TYPE_CHECKING` block ensures that `mypy-boto3-logs` is only a development dependency. Replace placeholder credentials with actual AWS configuration for real-world usage.

from typing import TYPE_CHECKING
import boto3

if TYPE_CHECKING:
    from mypy_boto3_logs.client import CloudWatchLogsClient
    from mypy_boto3_logs.type_defs import PutLogEventsResponseTypeDef


def get_logs_client() -> 'CloudWatchLogsClient':
    """Returns a type-hinted CloudWatchLogs client."""
    session = boto3.Session(
        aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', ''),
        aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', ''),
        region_name=os.environ.get('AWS_REGION', 'us-east-1')
    )
    return session.client('logs')


def put_log_events_example():
    """Example of using the type-hinted CloudWatchLogs client."""
    client = get_logs_client()
    response: PutLogEventsResponseTypeDef = client.put_log_events(
        logGroupName='my-log-group',
        logStreamName='my-log-stream',
        logEvents=[
            {'timestamp': 1678886400000, 'message': 'Hello from mypy-boto3-logs!'}
        ]
    )
    print(f"Sequence Token: {response.get('nextSequenceToken')}")

if __name__ == '__main__':
    import os
    # Set dummy AWS credentials for local testing or ensure they are in env vars
    # For actual use, configure boto3 credentials securely
    if not os.environ.get('AWS_ACCESS_KEY_ID'):
        os.environ['AWS_ACCESS_KEY_ID'] = 'TEST_ACCESS_KEY'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'TEST_SECRET_KEY'
        os.environ['AWS_REGION'] = 'us-east-1'

    put_log_events_example()

view raw JSON →