Boto3 cloudwatch

From wikinotes

CloudWatch is AWS's notifications/alarms system.

Connection

import boto3

session = boto3.session.Session()
client = session.client(
    'cloudwatch',
    region_name='us-east-1',  # !IMPORTANT! only SNS ARN's from this region are available from here
    aws_access_key_id='JLjk12H/j98afrz+LKJ28jlkJfjxlki12Vahlj12fZM',
    aws_secret_access_key='ALSKDJw3erukljasdf',
)
client.describe_alarms()

Alarms

Before you can create an alarm, you need:

  • an SNS topic (simple notification service - email, sms, ...)
  • a HealthCheck or other metric to monitor

Create/Update Alarm with this name

    alarm = {
        'AlarmName': 'HTTP: RESTAPI unreachable',
        'ComparisonOperator': 'LessThanThreshold',
        'EvaluationPeriods': 1,
        'MetricName': 'HealthCheckStatus',
        'Namespace': 'AWS/Route53',
        'Period': 60,
        'Statistic': 'Minimum',
        'Threshold': 1.0,
        'ActionsEnabled': True,
        'AlarmDescription': 'Alarm when REST-API not accessible over HTTP in region {}'.format(digitalocean_region),
        'AlarmActions': [ 'arn:aws:sns:<aws-region>:369511496789:support_email'],
        'Dimensions': [
            {'Name': 'HealthCheckId', 'Value': healthcheck_id}
        ],
    }
    reply = client.put_metric_alarm(**alarm)