Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a StatusCheckFailed alarm
import boto.ec2.cloudwatch
import sys
import os
AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_REGION = os.environ.get("AWS_EC2_REGION", "us-east-1")
TOPIC = 'YOUR_TOPIC'
def create_status_alarm(instance_id):
ec2_conn = boto.ec2.connect_to_region(AWS_REGION,
aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_SECRET)
cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
AWS_REGION,
aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_SECRET)
reservations = ec2_conn.get_all_instances(filters = {'instance-id': instance_id})
if reservations and reservations[0].instances:
instance = reservations[0].instances[0]
instance_name = instance.tags['Name']
else:
print "Invalid instance-id!"
sys.exit(1)
alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
connection = cloudwatch_conn,
name = instance_name + "-status-alarm",
metric = 'StatusCheckFailed',
namespace = 'AWS/EC2',
statistic = 'Maximum',
comparison = '>=',
description = 'status check for %s %s' % (instance_id, instance_name),
threshold = 1.0,
period = 60,
evaluation_periods = 2,
dimensions = {'InstanceId':instance_id},
alarm_actions = [TOPIC],
ok_actions = [TOPIC],
insufficient_data_actions = [TOPIC])
cloudwatch_conn.put_metric_alarm(alarm)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: create_status_alarm.py <instanceid>"
sys.exit(2)
create_status_alarm(sys.argv[1])
@jonathanwcrane

This comment has been minimized.

Copy link

commented Mar 13, 2015

Very nice. Helpful demonstration of something I'm building today. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.