我想在队列' myDownlinQueue'中创建一个警报(触发器)为ApproximateNumberOfMessagesVisible的警报,警报采取自动调整AutoScalingGroup的操作。但是根据我写的代码,我得到了标题中提到的错误。也许我做错了,但下面是导致此错误的代码。
# ==== AutoSCaling config =======================
autoscaling_group = {
"name": "myAG", #descriptive name for your auto scaling group
"min_size": 0 , #Minimum number of instances that should be running at all times
"max_size": 0 #Maximum number of instances that should be running at all times
}
lc_name = 'myLG' #Descriptive name for your launch configuration
#=================AMI to launch======================================================
as_ami = {
"id": "ami-******c", #The AMI ID of the instance your Auto Scaling group will launch
"VpcId" : "vpc-0c805575",
"security_groups": "sg-xxxxxxxa", #The security group(s) your instances will belong to
"instance_type": "t2.micro", #The size of instance that will be launched
"instance_monitoring": True #Indicated whether the instances will be launched with detailed monitoring enabled. Needed to enable CloudWatch
}
conn_reg = boto.ec2.connect_to_region(region_name=awsRegion)
zones = conn_reg.get_all_zones()
zoneStrings = []
for zone in zones:
zoneStrings.append(zone.name)
print " Available zones : " + zone.name
conn_vpc = boto.connect_vpc()
subnetids = conn_vpc.get_all_subnets()
conn_as = AutoScaleConnection(AWS_ACCESS_KEY,AWS_SECRET_KEY)
lc = LaunchConfiguration(name = lc_name,
image_id = as_ami["id"],
instance_type = as_ami["instance_type"],
user_data = "user-data.bls",
associate_public_ip_address = True,
instance_monitoring=as_ami["instance_monitoring"])
conn_as.create_launch_configuration(lc)
ag = AutoScalingGroup(group_name = autoscaling_group["name"],
availability_zones= zoneStrings,
vpc_zone_identifier = subnetList,
launch_config=lc, min_size = autoscaling_group["min_size"],
max_size = autoscaling_group["max_size"])
conn_as.create_auto_scaling_group(ag)
#=================Create Scaling Policies===================================
# Policy for scaling the number of servers up and down
scalingUpPolicy = ScalingPolicy(name = "myScaleUpPolicy",
adjustment_type ="ChangeInCapacity",
as_name=ag.name,
scaling_adjustment = numInstances ,
cooldown=180)
scalingDownPolicy = ScalingPolicy(name = "myScaleDownPolicy",
adjustment_type= "ExactCapacity",
as_name=ag.name,
scaling_adjustment= 0 ,
cooldown=180)
conn_as.create_scaling_policy(scalingUpPolicy)
conn_as.create_scaling_policy(scalingDownPolicy)
scalingUpPolicy = conn_as.get_all_policies(as_group="myAG", policy_names=["myScaleUpPolicy"])[0]
scalingDownPolicy = conn_as.get_all_policies(as_group="myAG",policy_names=["myScaleDownPolicy"])[0]
# =========== CloudWatch Connection =============
cw = connect_to_region(awsRegion)
# =========== SNS Connection ===================
sns = connect_to_region(awsRegion)
cw = CloudWatchConnection(AWS_ACCESS_KEY,AWS_SECRET_KEY)
sqs = SQSConnection(AWS_ACCESS_KEY,AWS_SECRET_KEY)
# ==== Alarm =================
numberOfMessages = 1
metric_object = cw.list_metrics(dimensions={"QueueName":"myDownlinkQueue"}, metric_name = "ApproximateNumberOfMessagesVisible",namespace = "AWS/SQS")
alarm_name = "myAlarm"
metric_object["ApproximateNumberOfMessagesVisible"].create_alarm(name =alarm_name,
comparison=">=",
threshold = numberOfMessages,
period = 60,
evaluation_periods = 1,
statistic = "Average",
alarm_actions=[scalingDownPolicy.policy_arn])
错误在最后一个方法中 - 创建一个警报,精确地为scalingDownPolicy.policy_arn
答案 0 :(得分:0)
OP误读文档或混淆。
根据boto2 cloudwatch.list_metric(),该项目将返回list
。
list_metrics(next_token =无,维度=无,metric_name =无, 命名空间=无)
返回记录数据的有效指标列表 可用。
因此metric_object["ApproximateNumberOfMessagesVisible"]
会犯错误TypeError : list indices must be integers not str
OTH,boto3 cloudwatch.list_object返回一个字典。它们不兼容。但是,没有字典键具有ApproximateNumberOfMessagesVisible。
{
'Metrics': [
{
'Namespace': 'string',
'MetricName': 'string',
'Dimensions': [
{
'Name': 'string',
'Value': 'string'
},
]
},
],
'NextToken': 'string'
}