Last active
December 11, 2015 04:38
-
-
Save snarkyboojum/4546268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import boto | |
import boto.ec2.cloudwatch | |
from boto.ec2.cloudwatch import MetricAlarm | |
access_key_id='YOUR_ACCESS_KEY_ID' | |
secret_access_key='YOUR_SECRET_ACCESS_KEY' | |
# make this instance able to sleep, i.e. able to shutdown if not utilised | |
def make_sleepy(cw_conn, region, instance_id): | |
print "Making instance", instance_id, "sleepy..." | |
# we build the 'stop' action ARN with region | |
shutdown_arn = 'arn:aws:automate:{0}:ec2:stop'.format(region) | |
alarm_name = 'ec2_shutdown_sleepy_{0}'.format(instance_id) | |
# define our alarm to shutdown the instance if it gets sleepy | |
# i.e. if CPU utilisation is less than 2% for 24 x 1 hr intervals | |
sleepy_alarm = MetricAlarm( | |
name=alarm_name, namespace='AWS/EC2', | |
metric='CPUUtilization', statistic='Average', | |
comparison='<', threshold='2', | |
period='3600', evaluation_periods=24, | |
alarm_actions=[shutdown_arn], | |
dimensions={'InstanceId':instance_id}) | |
# create the alarm.. Zzzz! | |
cw_conn.create_alarm(sleepy_alarm) | |
def main(): | |
# connect to all regions and find all instances we own | |
for region in boto.ec2.regions(): | |
print "Connecting to", region.endpoint, "..." | |
ec2_conn = region.connect(aws_access_key_id=access_key_id, | |
aws_secret_access_key=secret_access_key) | |
cw_conn = boto.ec2.cloudwatch.connect_to_region(region.name) | |
# make each instance in the region sleepy (or capable of sleep anyway) | |
for reservation in ec2_conn.get_all_instances(): | |
for instance in reservation.instances: | |
make_sleepy(cw_conn, region.name, instance.id) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./ec2_sleepy.py | |
Connecting to ec2.ap-southeast-1.amazonaws.com ... | |
Making instance i-8c47b3d8 sleepy... | |
Making instance i-d832c98c sleepy... | |
Making instance i-18e1284c sleepy... | |
Making instance i-2a0ea07e sleepy... | |
Making instance i-b852e9ec sleepy... | |
Making instance i-5a84fb0e sleepy... | |
Making instance i-0a0d4d5e sleepy... | |
Making instance i-8c3f61d8 sleepy... | |
Making instance i-422a1716 sleepy... | |
Connecting to ec2.ap-southeast-2.amazonaws.com ... | |
Making instance i-21fb9e1b sleepy... | |
Making instance i-bffa9f85 sleepy... | |
Making instance i-a1a1df9b sleepy... | |
Making instance i-9bb0f4a1 sleepy... | |
Making instance i-0128683b sleepy... | |
Making instance i-63377759 sleepy... | |
Making instance i-fd8bc5c7 sleepy... | |
Making instance i-7faffa45 sleepy... | |
Connecting to ec2.us-west-2.amazonaws.com ... | |
Making instance i-0fecc53c sleepy... | |
Connecting to ec2.us-east-1.amazonaws.com ... | |
Making instance i-c34b98a7 sleepy... | |
Making instance i-7e308706 sleepy... | |
Connecting to ec2.ap-northeast-1.amazonaws.com ... | |
Making instance i-4cb82f4f sleepy... | |
Connecting to ec2.us-west-1.amazonaws.com ... | |
Making instance i-e81f94ae sleepy... | |
Making instance i-86ceb1df sleepy... | |
Connecting to ec2.sa-east-1.amazonaws.com ... | |
Connecting to ec2.eu-west-1.amazonaws.com ... | |
Making instance i-11e7ad59 sleepy... | |
Making instance i-d777759f sleepy... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment