Ignoring case sensitivity when filtering with boto3

Question:

I’m trying to filter the list of EC2 instances that I get in my script. I want my search to be based on tags, as we tag instances based on their purpose (service machines, personal gateways, etc).

I use this:

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'])

The above works fine, but I’m not getting the correct amount of instances according to my counter.

I went and double checked it: in my EC2 console I get 361 instances based on that same tag:
enter image description here

When I run the above code, I get 335 instances (according to the counter I placed).

Then when I change the filter to use GW instead of gw, I get a 26 instances only, which add up to 361 (335 + 26).

I tried fixing it by simply adding another filter, like so:

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

This variation doesn’t return anything, so I guess I can’t use the same key with "different" values?

I tried stuff like {'Name': 'tag:Service', 'Values': ['gw'|'GW'] but | is not supported.

I would like to avoid searching for these tags inside a loop. I think utilizing the built-in filter inside of my paginator would be cleaner and simpler.

I’m not sure what other options I have. Why would the EC2 console be case insensitive, but the filter is not?

Edit:

turns out I wasn’t paying enough attention to the docs, I just needed: {'Name': 'tag:Service', 'Values': ['gw', 'GW']}

Asked By: Daniel

||

Answers:

From 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.

So it should be:

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