Type annotations for boto3 CloudWatch

1.42.82 · active · verified Fri Apr 10

mypy-boto3-cloudwatch provides type annotations for the `boto3` CloudWatch service, generated by `mypy-boto3-builder`. It enhances development with static type checking, autocompletion, and bug detection for `boto3` CloudWatch clients. The library is actively maintained with frequent updates to align with new `boto3` versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `boto3` CloudWatch client with explicit type hints from `mypy-boto3-cloudwatch` and how to use a generated TypeDef for a method's input parameters.

import boto3
from mypy_boto3_cloudwatch.client import CloudWatchClient
from mypy_boto3_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef

# Instantiate a CloudWatch client with type annotation
client: CloudWatchClient = boto3.client(
    "cloudwatch",
    region_name="us-east-1",
    aws_access_key_id="AKIATESTKEY", # Use os.environ.get('AWS_ACCESS_KEY_ID', '') in real code
    aws_secret_access_key="YOUR_SECRET_KEY" # Use os.environ.get('AWS_SECRET_ACCESS_KEY', '') in real code
)

# Example of using a TypeDef for a request payload
metric_data: PutMetricDataInputRequestTypeDef = {
    "Namespace": "MyApplication",
    "MetricData": [
        {
            "MetricName": "CustomMetric",
            "Dimensions": [
                {"Name": "Environment", "Value": "Production"}
            ],
            "Value": 10.5,
            "Unit": "Count",
        }
    ],
}

try:
    response = client.put_metric_data(**metric_data)
    print(f"Successfully put metric data: {response['ResponseMetadata']['HTTPStatusCode']}")
except Exception as e:
    print(f"Error putting metric data: {e}")

view raw JSON →