我正在尝试在boto3和Amazon ec2实例上学习cloudwatch警报,如何在自定义脚本中对其进行编程。
import boto3
import time
# Create CloudWatch client
cloudwatch = boto3.client('cloudwatch')
# Create alarm with actions enabled
cloudwatch.put_metric_alarm(
AlarmName='Web_Server_CPU_Utilization',
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=1,
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Period=60,
Statistic='Average',
Threshold=70.0,
ActionsEnabled=True,
AlarmActions=[
'arn:aws:swf:us-west-2:{CUSTOMER_ACCOUNT}:action/actions/AWS_EC2.InstanceId.Reboot/1.0'
],
AlarmDescription='Alarm when server CPU exceeds 70%',
Dimensions=[
{
'Name': 'InstanceId',
'Value': 'INSTANCE_ID'
},
],
Unit='Seconds'
)
但是在执行上述程序时出现错误
Traceback (most recent call last):
File "D:\expt2\cloudwatch alarm in action.py", line 27, in <module>
Unit='Seconds'
File "E:\installation2\python3\lib\site-packages\botocore\client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "E:\installation2\python3\lib\site-packages\botocore\client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the PutMetricAlarm operation: Unknown
此示例代码给了我错误,我该如何解决?
答案 0 :(得分:0)
ValidationError
(略有记载)here。
该错误是由AlarmActions
列表中格式错误的项目引起的。格式应为:
arn:aws:swf:区域:帐户ID :action / actions / AWS_EC2.InstanceId.Reboot / 1.0
与此同时
arn:aws:swf:us-west-2:{CUSTOMER_ACCOUNT}:action / actions / AWS_EC2.InstanceId.Reboot / 1.0
因此,您只需要更新即可在拥有{CUSTOMER_ACCOUNT}
的地方使用真实的AWS账户ID。