Skip to content

Commit

Permalink
added EC2 instances detection for ECS clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
meshuga committed May 23, 2020
1 parent 438162d commit f5033ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion shared/internal/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run(self):
if elasticsearch_domain['DomainStatus']['VPCOptions']['VPCId'] == self.vpc_options.vpc_id \
or ipvpc_found is True:
found += 1
message = message + "\nDomainId: {0} - DomainName: {1} - VpcId {2}".format(
message = message + "\nDomainId: {0} - DomainName: {1} -> VPC Id: {2}".format(
elasticsearch_domain['DomainStatus']['DomainId'],
elasticsearch_domain['DomainStatus']['DomainName'],
self.vpc_options.vpc_id
Expand Down
34 changes: 34 additions & 0 deletions shared/internal/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, vpc_options: VpcOptions):
def run(self):

client = self.vpc_options.client('ecs')
ec2_client = self.vpc_options.client('ec2')

clusters_list = client.list_clusters()
response = client.describe_clusters(
Expand Down Expand Up @@ -61,6 +62,39 @@ def run(self):
""" EC2 services require container instances, list of them should be fine for now """
pass

""" Looking for container instances - they are dynamically associated, so manual review is necessary """
list_paginator = client.get_paginator('list_container_instances')
list_pages = list_paginator.paginate(
cluster=data['clusterName']
)
for list_page in list_pages:
container_instances = client.describe_container_instances(
cluster=data['clusterName'],
containerInstances=list_page['containerInstanceArns']
)
ec2_ids = []
for instance_details in container_instances['containerInstances']:
ec2_ids.append(instance_details['ec2InstanceId'])
paginator = ec2_client.get_paginator('describe_instances')
pages = paginator.paginate(
InstanceIds=ec2_ids
)
for page in pages:
for reservation in page['Reservations']:
for instance in reservation['Instances']:
for network_interfaces in instance['NetworkInterfaces']:
if network_interfaces['VpcId'] == self.vpc_options.vpc_id:
found += 1
message = message + "\nclusterName: {} -> Instance Id: {} -> Subnet id: {} -> VPC id {}".format(
data["clusterName"],
instance['InstanceId'],
network_interfaces['SubnetId'],
self.vpc_options.vpc_id
)
pass
pass
pass


message_handler("Found {0} ECS Cluster using VPC {1} {2}".format(str(found), self.vpc_options.vpc_id, message),'OKBLUE')

Expand Down

0 comments on commit f5033ff

Please sign in to comment.