Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

python - list of all autoscaling groups whose scaling policy on CPU is below certain threshold in aws using boto3

i have bunch of ASGs want to use boto3 to get all ASG Names whose scaling policies on CPU are less than certain threshold (i.e All ASGs where scaling policies is set and value < 70) for some reason getting only one asg and facing issue in appending to dict


client = boto3.client('autoscaling', region_name='region_name')
response = client.describe_auto_scaling_groups()
#print(response)

# function to get all asg_names 
def get_asg_names():
    for asg_name in response['AutoScalingGroups']:
        asg_list = ((asg_name['AutoScalingGroupName']))
        #print(asg_list)
        get_scaling_policy_values(asg_list)

#print((asg_list))
asg_dict_map = dict()

def get_scaling_policy_values(asg_list):
    response2 = client.describe_policies(AutoScalingGroupName = asg_list)
    #print(response2)
    for policy_name in response2['ScalingPolicies']:
        asg_name_with_policy = policy_name['AutoScalingGroupName']
        target_value = policy_name['TargetTrackingConfiguration']['TargetValue']
        asg_dict_map[asg_names_with] =  asg_dict_map[target_value]
        #asg_dict_map['key'] = policy_name['AutoScalingGroupName']
        #asg_dict_map['value'] = policy_name['TargetTrackingConfiguration']['TargetValue']

get_asg_names()
print(asg_dict_map)```
question from:https://stackoverflow.com/questions/65850805/list-of-all-autoscaling-groups-whose-scaling-policy-on-cpu-is-below-certain-thre

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would:

  • Use describe_auto_scaling_groups() to obtain a list of Auto Scaling groups
  • For each of them, call describe_policies() passing in the name of the Auto Scaling group
  • Examine the configuration returned and perform your comparisons

Slightly modified version of your code:

import boto3

asg_dict_map = dict()

client = boto3.client('autoscaling')
response = client.describe_auto_scaling_groups()

for asg in response['AutoScalingGroups']:
    asg_name = asg['AutoScalingGroupName']

    response2 = client.describe_policies(AutoScalingGroupName = asg_name)
    for policy in response2['ScalingPolicies']:
        policy_name = policy['AutoScalingGroupName']
        target_value = policy['TargetTrackingConfiguration']['TargetValue']
        asg_dict_map[policy_name] = target_value

print(asg_dict_map)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...