使用 boto3 过滤时忽略区分大小写

Ignoring case sensitivity when filtering with boto3

我正在尝试过滤我在脚本中获得的 EC2 实例列表。我希望我的搜索基于标签,因为我们根据实例的用途(服务机器、个人网关等)标记实例。

我用这个:

client = boto3.client('ec2', region_name='eu-west-1')

results = (
   client.get_paginator('describe_instances')
   .paginate(
       Filters=[
           {'Name': 'tag:Service', 'Values': ['gw']}
           ]
           )
   .build_full_result()
)
counter=0

for result in results['Reservations']:
    counter+=1
    print(counter, result['Instances'][0]['InstanceId'])

以上工作正常,但根据我的计数器,我没有得到正确数量的实例。

我仔细检查了一下:在我的 EC2 控制台中,我得到了 361 个基于相同标签的实例:

当我运行上面的代码时,我得到了335个实例(根据我放置的计数器)。

然后当我将过滤器更改为使用 GW 而不是 gw 时,我只得到 26 个实例,加起来是 361 (335 + 26)。

我尝试通过简单地添加另一个过滤器来修复它,如下所示:

results = (
   client.get_paginator('describe_instances')
   .paginate(
       Filters=[
           {'Name': 'tag:Service', 'Values': ['gw']},
           {'Name': 'tag:Service', 'Values': ['GW']}
           ]
           )
   .build_full_result()
)

这个变体没有 return 任何东西,所以我想我不能使用具有“不同”值的相同键?

我试过 {'Name': 'tag:Service', 'Values': ['gw'|'GW'] 之类的东西,但不支持 |

我想避免在循环中搜索这些标签。我认为在我的分页器中使用内置的 filter 会更干净、更简单。

我不确定我还有哪些其他选择。为什么 EC2 控制台不区分大小写,但筛选器不区分大小写?

编辑:

原来我没有对文档给予足够的重视,我只是需要:{'Name': 'tag:Service', 'Values': ['gw', 'GW']}

来自docs

If you specify multiple values for a filter, the values are joined with an OR , and the request returns all results that match any of the specified values.

所以应该是:

{'Name': 'tag:Service', 'Values': ['gw','GW']}