Skip to content

Commit

Permalink
Updated the long running instances list (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored Jul 4, 2024
1 parent e993ea3 commit 71b2bbc
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions cloudsensei/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
from ast import literal_eval
from time import time

import boto3
Expand All @@ -25,8 +26,33 @@ def __init__(self):
self.__ec2_client = boto3.client('ec2', region_name='us-east-1')
self.__iam_client = boto3.client('iam')
self.__resource_days = int(os.environ.get('RESOURCE_DAYS', 7))
self.__block_list_instances_name = literal_eval(os.environ.get('BLOCK_LIST_INSTANCES_NAMES', '[]'))
self.__users_mapping_list = self.get_user_mapping()

def get_tag_value_from_tags(self, tags: list, tag_name: str, cast_type: str = 'str',
default_value: any = '') -> any:
"""
This method returns the tag value inputted by tag_name
:param tags:
:param tag_name:
:param cast_type:
:param default_value:
:return:
"""
if tags:
for tag in tags:
key = tag.get('Key').lower().replace("_", '').replace("-", '').strip()
if key == tag_name.lower():
if cast_type:
if cast_type == 'int':
return int(tag.get('Value').split()[0].strip())
elif cast_type == 'float':
return float(tag.get('Value').strip())
else:
return str(tag.get('Value').strip())
return tag.get('Value').strip()
return default_value

def get_user_mapping(self):
"""
This method returns the user mappings
Expand Down Expand Up @@ -55,10 +81,11 @@ def __get_all_instances(self):
:return:
"""
resource_list = []
resources = self.__ec2_client.describe_instances()
filter_list = [{'Name': 'instance-state-name', 'Values': ['running']}]
resources = self.__ec2_client.describe_instances(Filters=filter_list)
resource_list.extend(resources['Reservations'])
while 'NextToken' in resources.keys():
resources = self.__ec2_client.describe_instances(NextToken=resources['NextToken'])
resources = self.__ec2_client.describe_instances(NextToken=resources['NextToken'],Filters=filter_list)
resource_list.extend(resources['Reservations'])
return resource_list

Expand All @@ -79,20 +106,15 @@ def get_resources(self):
skip = False
launch_time = resource.get('LaunchTime').date()
days = (current_datetime - launch_time).days
if days > self.__resource_days:
user = name = None
ticket_tags = ''
for tag in resource.get('Tags', []):
tag_key = tag.get('Key').lower()
if tag_key.lower() == 'cloudsensei':
skip = True
break
if tag_key == 'user':
user = tag.get('Value')
if tag_key == 'ticketid':
ticket_tags = tag.get('Value')
elif tag_key == 'name':
name = tag.get('Value')
tags = resource.get('Tags', [])
name = self.get_tag_value_from_tags(tag_name='Name', tags=tags)
if days > self.__resource_days and \
name not in self.__block_list_instances_name:
cloudsensei = self.get_tag_value_from_tags(tag_name='cloudsensei', tags=tags)
if cloudsensei:
break
ticket_tags = self.get_tag_value_from_tags(tag_name='ticketid', tags=tags)
user = self.get_tag_value_from_tags(tag_name='user', tags=tags)
if not skip and user:
user = user.lower()
if user in self.__users_mapping_list:
Expand Down Expand Up @@ -136,7 +158,6 @@ def organize_message_to_send_slack(self, resources_list: dict):
"type": "section",
"fields": [
{"type": "mrkdwn", "text": f"{str(resources.get(item))}"}
if item != 'User' else {"type": "mrkdwn", "text": f"@{str(resources.get(item))}"}
for item in self.KEYS_LIST]}
)
rows.append(divider)
Expand Down Expand Up @@ -164,6 +185,14 @@ def organize_message_to_send_slack(self, resources_list: dict):
{"type": "mrkdwn", "text": f"{item}"} for item in self.KEYS_LIST
]
}]]
if not item_blocks:
slack_message_block.append([{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "No long running instances"
}
}])
for block in item_blocks:
slack_message_block.append(block)
return slack_message_block
Expand All @@ -174,6 +203,8 @@ def organize_message_to_seng_mail(self, resources_list: dict):
:param resources_list:
:return:
"""
if not resources_list:
return ''
with open('email_template.j2') as template:
template = Template(template.read())
body = template.render({'resources_list': resources_list, 'keys_list': self.KEYS_LIST})
Expand All @@ -189,6 +220,8 @@ def send_email(self, organized_ec2_data):
This method send email
:return:
"""
if not organized_ec2_data:
return 204, "No long running instances"
response = send_email_with_ses(body=organized_ec2_data, subject=self.__subject, to=os.environ.get('TO_ADDRESS'),
cc=os.environ.get('CC_ADDRESS'))
if response:
Expand Down

0 comments on commit 71b2bbc

Please sign in to comment.