簡體   English   中英

使用 boto3 在 aws 中其 CPU 擴展策略低於特定閾值的所有自動擴展組的列表

[英]list of all autoscaling groups whose scaling policy on CPU is below certain threshold in aws using boto3

我有一堆 ASG 想要使用 boto3 來獲取所有在 CPU 上的擴展策略小於某個閾值的 ASG 名稱(即所有設置了擴展策略且值 < 70 的 ASG),由於某種原因只得到一個 asg 並面臨附加問題聽寫


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)```

你會:

  • 使用describe_auto_scaling_groups()獲取 Auto Scaling 組的列表
  • 對於它們中的每一個,調用describe_policies()並傳入 Auto Scaling 組的名稱
  • 檢查返回的配置並進行比較

您的代碼稍作修改的版本:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM