Dmitry Petrov Dmitry Petrov - 2 months ago 10
Python Question

Python boto: filter on tag and value

Filtering by tag key works just fine:

ec2.get_all_instances(filters={'tag-key': 'MachineType'})


How can I filter by key and value?

"MachineType=DB"

Answer

Boto

ec2.get_all_instances(filters={"tag:MachineType" : "DB"})

Boto3

import boto3

ec2 = boto3.resource('ec2')
inst_filter = [{'Name':'tag: MachineType', 'Values':['DB']}]
insts = list(ec2.instances.filter(Filters=inst_filter))
for inst in insts:
  print inst.id
Comments