我一直无法找到一个简单的例子,告诉我如何使用boto来使用闹钟来终止Amazon EC2实例(不使用AutoScaling)。我想在10分钟内终止CPU使用率小于1%的特定实例。如何设置警报来终止使用boto的EC2实例?
这是我到目前为止已经试过:
import boto.ec2
import boto.ec2.cloudwatch
from boto.ec2.cloudwatch import MetricAlarm
conn = boto.ec2.connect_to_region("us-east-1", aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
cw = boto.ec2.cloudwatch.connect_to_region("us-east-1", aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
reservations = conn.get_all_instances()
for r in reservations:
for inst in r.instances:
alarm = boto.ec2.cloudwatch.MetricAlarm(name='TestAlarm', description='This is a test alarm.', namespace='AWS/EC2', metric='CPUUtilization', statistic='Average', comparison='<=', threshold=1, period=300, evaluation_periods=2, dimensions={'InstanceId':[inst.id]}, alarm_actions=['arn:aws:automate:us-east-1:ec2:terminate'])
cw.put_metric_alarm(alarm)
不幸的是它给了我这个错误:
dimensions={'InstanceId':[inst.id]}, alarm_actions=['arn:aws:automate:us-east-1:ec2:terminate']) TypeError: init() got an unexpected keyword argument 'alarm_actions'
我敢肯定,这是一些简单的我失踪。
此外,我没有使用CloudFormation,因此我无法使用AutoScaling功能。这是因为我不希望警报在整个组中使用度量标准,而只是针对特定实例使用度量标准,并且仅终止该特定实例(而不是该组中的任何实例)。
在此先感谢您的帮助!
现在我得到的错误:'cw.put_metric_alarm(报警) 文件“/usr/lib/python2.6/site-packages/boto-2.0-py2.6.egg/boto/ec2/cloudwatch/__init__.py”,第545行,在put_metric_alarm中 'Dimensions.member。%s 。%s') 文件“/usr/lib/python2.6/site-packages/boto-2.0-py2.6.egg/boto/ec2/cloudwatch/__init__.py”,第237行,在build_list_params中 参数[标签%i] =项目 TypeError:没有足够的格式字符串参数 – Chewy734
原来我使用boto 2.0,这不是最新版本。一旦我更新到2.25.0,它使用我原来的方式运行良好。虽然谢谢! – Chewy734
那么,我的答案也能工作吗? – Rico