调用Boto3 describe_auto_scaling_groups出现Filters参数未知错误如何解决
问题原因
你当前使用的boto3及依赖的botocore版本过低,describe_auto_scaling_groups接口的Filters参数属于后续新增的功能,旧版本的接口确实不支持该入参,仅支持AutoScalingGroupNames、NextToken、MaxRecords三个参数,所以会触发参数校验报错。
解决方案
无需传入AutoScalingGroupNames参数,两种实现方式如下:
方案1:升级boto3版本(推荐)
直接升级boto3和botocore到最新版本即可兼容你原有的查询写法,升级命令:
pip install --upgrade boto3 botocore
升级完成后你现有的代码无需修改即可正常执行,得到符合标签筛选条件的ASG列表。
方案2:全量查询后本地过滤(适用于无法升级依赖的场景)
如果当前环境不允许升级依赖包,可以先拉取所有ASG的全量数据,再在本地对标签进行筛选,同时要处理分页逻辑避免ASG数量较多时数据拉取不全,示例代码如下:
import boto3 region = "替换为你的目标区域" client = boto3.client('autoscaling', region_name=region) all_asgs = [] next_token = '' # 分页拉取全量ASG while True: if next_token: resp = client.describe_auto_scaling_groups(NextToken=next_token, MaxRecords=100) else: resp = client.describe_auto_scaling_groups(MaxRecords=100) all_asgs.extend(resp['AutoScalingGroups']) if 'NextToken' in resp: next_token = resp['NextToken'] else: break # 本地按标签规则过滤 filtered_asgs = [] for asg in all_asgs: tag_dict = {tag['Key']: tag['Value'] for tag in asg.get('Tags', [])} if tag_dict.get('ecs_cluster') == 'testCluster' and tag_dict.get('cluster-name') == 'ciCluster': filtered_asgs.append(asg)
最终filtered_asgs就是符合筛选条件的ASG列表。
内容的提问来源于stack exchange,提问作者Deepanshu Kalra
相关产品推荐
相关产品推荐

