Dmitry Petrov Dmitry Petrov - 1 year ago 125
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?


Answer Source


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


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:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download