From 9feabf54796c686147ef44410b424649d7a4c245 Mon Sep 17 00:00:00 2001 From: tony-dot-sh <11530086+tony-dot-sh@users.noreply.github.com> Date: Mon, 15 Feb 2021 09:38:40 -0700 Subject: [PATCH] Enhancement: implement EC2 instance filtering by `subnet-id` (#3694) Co-authored-by: Tony Greising-Murschel --- moto/ec2/utils.py | 1 + tests/test_ec2/test_instances.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 76d788bd4..e47f06b85 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -379,6 +379,7 @@ filter_dict_attribute_mapping = { "network-interface.private-dns-name": "private_dns", "private-dns-name": "private_dns", "owner-id": "owner_id", + "subnet-id": "subnet_id", } diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 62a4b7350..ecaadadf1 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -606,6 +606,29 @@ def test_get_instances_filtering_by_instance_group_id(): reservations[0]["Instances"].should.have.length_of(1) +@mock_ec2 +def test_get_instances_filtering_by_subnet_id(): + client = boto3.client("ec2", region_name="us-east-1") + + vpc_cidr = ipaddress.ip_network("192.168.42.0/24") + subnet_cidr = ipaddress.ip_network("192.168.42.0/25") + + resp = client.create_vpc(CidrBlock=str(vpc_cidr),) + vpc_id = resp["Vpc"]["VpcId"] + + resp = client.create_subnet(CidrBlock=str(subnet_cidr), VpcId=vpc_id) + subnet_id = resp["Subnet"]["SubnetId"] + + client.run_instances( + ImageId=EXAMPLE_AMI_ID, MaxCount=1, MinCount=1, SubnetId=subnet_id, + ) + + reservations = client.describe_instances( + Filters=[{"Name": "subnet-id", "Values": [subnet_id]}] + )["Reservations"] + reservations.should.have.length_of(1) + + @mock_ec2_deprecated def test_get_instances_filtering_by_tag(): conn = boto.connect_ec2()