Shows how to use the AWS SDK for Python (Boto3) with Amazon CloudWatch to manage custom metrics and alarms.
CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes.
- Managing alarms and metrics
(
usage_demo)
- Creating an alarm that watches a metric
(
put_metric_alarm) - Deleting alarms that are watching a metric
(
delete_alarms) - Enabling and disabling actions on an alarm
(
enable_alarm_actions,disable_alarm_actions) - Getting statistics for a metric within a time span
(
get_metric_statistics) - Getting the alarms that are watching a metric
(
describe_alarms_for_metric) - Listing metrics
(
list_metrics) - Sending a set of data to a metric
(
put_metric_data_data_set) - Sending a single data value to a metric
(
put_metric_data)
- As an AWS best practice, grant this code least privilege, or only the permissions required to perform a task. For more information, see Grant Least Privilege in the AWS Identity and Access Management User Guide.
- This code has not been tested in all AWS Regions. Some AWS services are available only in specific Regions. For more information, see the AWS Region Table on the AWS website.
- Running this code might result in charges to your AWS account.
- You must have an AWS account, and have your default credentials and AWS Region configured as described in the AWS Tools and SDKs Shared Configuration and Credentials Reference Guide.
- Python 3.8 or later
- Boto3 1.14.47 or later
- PyTest 5.3.5 or later (to run unit tests)
Run this example at a command prompt with the following command.
python cloudwatch_basics.py
The example contains the following file.
cloudwatch_basics.py
Shows how to use Amazon CloudWatch APIs to create and manage custom metrics and
alarms. The CloudWatchWrapper class encapsulates CloudWatch functions. The
usage_demo script uses the CloudWatchWrapper class to:
- Create a custom metric and alarm
- Send data to the metric
- Get statistics for the metric
- Check the state of the alarm
The unit tests in this module use the botocore Stubber. The Stubber captures requests before they are sent to AWS, and returns a mocked response. To run all of the tests, run the following command in your [GitHub root]/python/example_code/cloudwatch folder.
python -m pytest
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0