filtering instances by name with boto3
I recently had a need to get a list of EC2 instance ID's by instance name using boto3. Most of the examples I found just make an unfiltered call to describe_instances()
and iterate over the results but I wasn't thrilled with that approach. The docs do have some example code but it wasn't obvious to me how do what I wanted and took a few tries to work out the approach.
If Google brought you here I hope this saves you some time!
import boto3
ec2 = boto3.client('ec2')
filters = [{
'Name': 'tag:Name',
'Values': ['instance_one', 'instance_two']
}]
reservations = ec2.describe_instances(Filters=filters)