Type Annotations for aiobotocore CloudWatch

3.4.0 · active · verified Mon Apr 13

This library provides comprehensive type annotations (stubs) for the `aiobotocore` CloudWatch service client. It enables static type checkers like MyPy to validate the usage of `aiobotocore.client.CloudWatch` methods, parameters, and return types, significantly enhancing code quality and reducing runtime errors. Generated by `mypy-boto3-builder`, it is updated frequently to align with the latest AWS API definitions and `aiobotocore` releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `types-aiobotocore-cloudwatch` for type hinting an `aiobotocore` CloudWatch client. It creates a client, prepares metric data using a `TypeDef`, and calls `put_metric_data` with full type safety.

import asyncio
import os
from aiobotocore.session import get_session
from types_aiobotocore_cloudwatch.client import CloudWatchClient
from types_aiobotocore_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef

async def put_cloudwatch_metric():
    session = get_session()
    # Ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION are set as environment variables
    async with session.create_client(
        "cloudwatch",
        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")
    ) as client:
        # client is automatically type-hinted as CloudWatchClient by static analysis
        # For explicit hinting:
        cloudwatch_client: CloudWatchClient = client

        metric_data: PutMetricDataInputRequestTypeDef = {
            "Namespace": "MyApplication",
            "MetricData": [
                {
                    "MetricName": "RequestCount",
                    "Dimensions": [
                        {"Name": "Environment", "Value": "Production"},
                    ],
                    "Value": 1.0,
                    "Unit": "Count",
                },
            ],
        }

        response = await cloudwatch_client.put_metric_data(
            Namespace=metric_data["Namespace"],
            MetricData=metric_data["MetricData"]
        )
        print(f"PutMetricData response: {response}")

if __name__ == "__main__":
    # Example usage: set dummy env vars if not truly interacting with AWS
    os.environ.setdefault("AWS_ACCESS_KEY_ID", "DUMMY_KEY")
    os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "DUMMY_SECRET")
    os.environ.setdefault("AWS_REGION", "us-east-1")
    asyncio.run(put_cloudwatch_metric())

view raw JSON →