diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 841444b8d..7dfa3ea03 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -98,10 +98,13 @@ def passes_filter_dict(instance, filter_dict): def filter_reservations(reservations, filter_dict): + result = [] for reservation in reservations: new_instances = [] for instance in reservation.instances: if passes_filter_dict(instance, filter_dict): new_instances.append(instance) - reservation.instances = new_instances - return reservations + if new_instances: + reservation.instances = new_instances + result.append(reservation) + return result diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 691e7908f..5244afd4d 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -96,6 +96,9 @@ def test_get_instances_filtering_by_state(): instance_ids = [instance.id for instance in reservations[0].instances] instance_ids.should.equal([instance2.id]) + reservations = conn.get_all_instances([instance2.id], filters={'instance-state-name': 'terminating'}) + list(reservations).should.equal([]) + # get_all_instances should still return all 3 reservations = conn.get_all_instances() reservations[0].instances.should.have.length_of(3)