Amazon ec2 如何设置警报以使用boto终止EC2实例?

Amazon ec2 如何设置警报以使用boto终止EC2实例?,amazon-ec2,alarm,boto,terminate,amazon-cloudwatch,Amazon Ec2,Alarm,Boto,Terminate,Amazon Cloudwatch,我找不到一个简单的例子来演示如何使用boto来终止AmazonEC2实例,该实例使用报警(而不使用自动缩放)。我想终止CPU使用率低于1%的特定实例10分钟 以下是我到目前为止所做的尝试: 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=AC

我找不到一个简单的例子来演示如何使用boto来终止AmazonEC2实例,该实例使用报警(而不使用自动缩放)。我想终止CPU使用率低于1%的特定实例10分钟

以下是我到目前为止所做的尝试:

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)
导入boto.ec2
导入boto.ec2.cloudwatch
从boto.ec2.cloudwatch导入MetricAlarm
conn=boto.ec2.将\u连接到\u区域(“us-east-1”,aws\u访问\u密钥\u id=access\u密钥,aws\u secret\u访问\u密钥=secret\u密钥)
cw=boto.ec2.cloudwatch.connect_到_区域(“us-east-1”,aws_访问_密钥_id=access_密钥,aws_secret_访问_密钥=secret_密钥)
保留=连接获取所有实例()
对于保留中的r:
对于r.实例中的inst:

报警=boto.ec2.cloudwatch.MetricAlarm(name='TestAlarm',description='This is a test alarm',namespace='AWS/EC2',metric='CPUUtilization',statistic='Average',comparization='p'>报警动作不会通过维度传递,而是作为属性添加到您正在使用的MetricAlarm对象中。在代码中,您需要执行以下操作:

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.add_alarm_action('arn:aws:automate:us-east-1:ec2:terminate')
cw.put_metric_alarm(alarm)

alarm=boto.ec2.cloudwatch.MetricAlarm(name='TestAlarm',description='This is a test alarm',namespace='AWS/ec2',metric='CPUUtilization',statistic='Average',comparison='现在我得到了一个错误:
cw.put\u metric\u报警(报警)文件)/usr/lib/python2.6/site packages/boto-2.0-py2.6.egg/boto/ec2/cloudwatch/\u init.py,第545行,输入度量报警“Dimensions.member.%s.%s”)文件“/usr/lib/python2.6/site packages/boto-2.0-py2.6.egg/boto/ec2/cloudwatch/uuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu=item TypeError:format string的参数不足
原来我使用的是boto 2.0,它不是最新版本。一旦我更新到2.25.0,它就可以用我原来的方式工作了。不过,谢谢!为了将来参考,这里列出了可用的自动报警操作:@Rico对于我的情况,EC2实例位于ins下EMR的实例组。我可以通过引导操作在给定的instanceId和“CPUUtilization”(无论您提到什么)上创建警报。但是,在引导时间内,给定instanceId没有维度。您能为我的用例建议一些解决方案吗?