Skip to content

Commit c96c292

Browse files
authored
Improve listing performance (#272)
1 parent 4398cff commit c96c292

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.8.7 (unreleased)
2+
------------------
3+
4+
* Improve instance listing performance in cases when there are many instances connecged to AWS Systems Manager
5+
16
0.8.6 (2020-01-17)
27
------------------
38

aws_gate/list.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
DEFAULT_LIST_OUTPUT,
1212
)
1313
from aws_gate.decorators import valid_aws_region, valid_aws_profile
14-
from aws_gate.utils import get_aws_client, get_aws_resource, get_instance_details
14+
from aws_gate.utils import (
15+
get_aws_client,
16+
get_aws_resource,
17+
get_multiple_instance_details,
18+
)
1519

1620
logger = logging.getLogger(__name__)
1721

@@ -85,10 +89,7 @@ def list_instances(
8589
for instance in response["InstanceInformationList"]:
8690
instance_ids.append(instance["InstanceId"])
8791

88-
instance_details = []
89-
for instance_id in instance_ids:
90-
instance_details.append(get_instance_details(instance_id=instance_id, ec2=ec2))
91-
92+
instance_details = get_multiple_instance_details(instance_ids=instance_ids, ec2=ec2)
9293
print(
9394
serialize(instance_details, output_format=output_format, fields=fields).rstrip()
9495
)

aws_gate/utils.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,33 @@ def fetch_instance_details_from_config(
177177

178178

179179
def get_instance_details(instance_id, ec2=None):
180-
filters = [{"Name": "instance-id", "Values": [instance_id]}]
180+
return get_multiple_instance_details(instance_ids=[instance_id], ec2=ec2)[0]
181181

182+
183+
def get_multiple_instance_details(instance_ids, ec2=None):
182184
try:
183-
ec2_instance = list(ec2.instances.filter(Filters=filters))[0]
185+
ec2_instances = list(ec2.instances.filter(InstanceIds=instance_ids))
184186
except botocore.exceptions.ClientError:
185187
raise AWSConnectionError
186188

187189
instance_name = None
188-
for tag in ec2_instance.tags:
189-
if tag["Key"] == "Name":
190-
instance_name = tag["Value"]
191-
192-
return {
193-
"instance_id": instance_id,
194-
"instance_name": instance_name,
195-
"availability_zone": ec2_instance.placement["AvailabilityZone"],
196-
"vpc_id": ec2_instance.vpc_id,
197-
"private_ip_address": ec2_instance.private_ip_address or None,
198-
"public_ip_addess": ec2_instance.public_ip_address or None,
199-
"private_dns_name": ec2_instance.private_dns_name or None,
200-
"public_dns_name": ec2_instance.public_dns_name or None,
201-
}
190+
instance_details = []
191+
for ec2_instance in ec2_instances:
192+
for tag in ec2_instance.tags:
193+
if tag["Key"] == "Name":
194+
instance_name = tag["Value"]
195+
196+
instance_details.append(
197+
{
198+
"instance_id": ec2_instance.id,
199+
"instance_name": instance_name,
200+
"availability_zone": ec2_instance.placement["AvailabilityZone"],
201+
"vpc_id": ec2_instance.vpc_id,
202+
"private_ip_address": ec2_instance.private_ip_address or None,
203+
"public_ip_addess": ec2_instance.public_ip_address or None,
204+
"private_dns_name": ec2_instance.private_dns_name or None,
205+
"public_dns_name": ec2_instance.public_dns_name or None,
206+
}
207+
)
208+
209+
return instance_details

0 commit comments

Comments
 (0)