| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-cw-creating-alarms: |
| 12 |
|
| 13 |
#################################### |
| 14 |
Creating alarms in Amazon CloudWatch |
| 15 |
#################################### |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get basic information about your CloudWatch alarms |
| 20 |
|
| 21 |
* Create and delete a CloudWatch alarm |
| 22 |
|
| 23 |
The scenario |
| 24 |
============ |
| 25 |
|
| 26 |
An alarm watches a single metric over a time period you specify, and performs one or more actions |
| 27 |
based on the value of the metric relative to a given threshold over a number of time periods. |
| 28 |
|
| 29 |
In this example, Python code is used to create alarms in CloudWatch. The code |
| 30 |
uses the AWS SDK for Python to create alarms using these methods of the AWS.CloudWatch client class: |
| 31 |
|
| 32 |
* `paginate(StateValue='INSUFFICIENT_DATA') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.paginate>`_. |
| 33 |
|
| 34 |
* `put_metric_alarm <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_alarm>`_. |
| 35 |
|
| 36 |
* `delete_alarms <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.delete_alarms>`_. |
| 37 |
|
| 38 |
For more information about CloudWatch alarms, see `Creating Amazon CloudWatch Alarms <http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html>`_ |
| 39 |
in the *Amazon CloudWatch User Guide*. |
| 40 |
|
| 41 |
All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_. |
| 42 |
|
| 43 |
Prerequisite tasks |
| 44 |
================= |
| 45 |
|
| 46 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 47 |
|
| 48 |
Describe alarms |
| 49 |
=============== |
| 50 |
|
| 51 |
The example below shows how to: |
| 52 |
|
| 53 |
* List metric alarms for insufficient data using |
| 54 |
`paginate(StateValue='INSUFFICIENT_DATA') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.paginate>`_. |
| 55 |
|
| 56 |
For more information about paginators see, :doc:`paginators` |
| 57 |
|
| 58 |
Example |
| 59 |
------- |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
import boto3 |
| 64 |
|
| 65 |
# Create CloudWatch client |
| 66 |
cloudwatch = boto3.client('cloudwatch') |
| 67 |
|
| 68 |
# List alarms of insufficient data through the pagination interface |
| 69 |
paginator = cloudwatch.get_paginator('describe_alarms') |
| 70 |
for response in paginator.paginate(StateValue='INSUFFICIENT_DATA'): |
| 71 |
print(response['MetricAlarms']) |
| 72 |
|
| 73 |
Create an alarm for a CloudWatch Metric alarm |
| 74 |
============================================= |
| 75 |
|
| 76 |
Create or update an alarm and associate it with the specified metric alarm. Optionally, this operation |
| 77 |
can associate one or more Amazon SNS resources with the alarm. |
| 78 |
|
| 79 |
When this operation creates an alarm, the alarm state is immediately set to :code:`INSUFFICIENT_DATA`. |
| 80 |
The alarm is evaluated and its state is set appropriately. Any actions associated with the state are |
| 81 |
then executed. |
| 82 |
|
| 83 |
When you update an existing alarm, its state is left unchanged, but the update completely overwrites |
| 84 |
the previous configuration of the alarm. |
| 85 |
|
| 86 |
The example below shows how to: |
| 87 |
|
| 88 |
* Create or update a metric alarm using |
| 89 |
`put_metric_alarm <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_alarm>`_. |
| 90 |
|
| 91 |
Example |
| 92 |
------- |
| 93 |
|
| 94 |
.. code-block:: python |
| 95 |
|
| 96 |
import boto3 |
| 97 |
|
| 98 |
# Create CloudWatch client |
| 99 |
cloudwatch = boto3.client('cloudwatch') |
| 100 |
|
| 101 |
# Create alarm |
| 102 |
cloudwatch.put_metric_alarm( |
| 103 |
AlarmName='Web_Server_CPU_Utilization', |
| 104 |
ComparisonOperator='GreaterThanThreshold', |
| 105 |
EvaluationPeriods=1, |
| 106 |
MetricName='CPUUtilization', |
| 107 |
Namespace='AWS/EC2', |
| 108 |
Period=60, |
| 109 |
Statistic='Average', |
| 110 |
Threshold=70.0, |
| 111 |
ActionsEnabled=False, |
| 112 |
AlarmDescription='Alarm when server CPU exceeds 70%', |
| 113 |
Dimensions=[ |
| 114 |
{ |
| 115 |
'Name': 'InstanceId', |
| 116 |
'Value': 'INSTANCE_ID' |
| 117 |
}, |
| 118 |
], |
| 119 |
Unit='Seconds' |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
Delete an alarm |
| 124 |
=============== |
| 125 |
|
| 126 |
Delete the specified alarms. In the event of an error, no alarms are deleted. |
| 127 |
|
| 128 |
The example below shows how to: |
| 129 |
|
| 130 |
* Delete a metric alarm using |
| 131 |
`delete_alarms <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.delete_alarms>`_. |
| 132 |
|
| 133 |
Example |
| 134 |
------- |
| 135 |
|
| 136 |
.. code-block:: python |
| 137 |
|
| 138 |
import boto3 |
| 139 |
|
| 140 |
# Create CloudWatch client |
| 141 |
cloudwatch = boto3.client('cloudwatch') |
| 142 |
|
| 143 |
# Delete alarm |
| 144 |
cloudwatch.delete_alarms( |
| 145 |
AlarmNames=['Web_Server_CPU_Utilization'], |
| 146 |
) |
| 147 |
|
| 148 |
|