Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Better EC2 launch time (#131)
Browse files Browse the repository at this point in the history
* Changed EC2 launch time source to ENI attach time

* Changelog update
  • Loading branch information
mlevit authored Dec 5, 2022
1 parent 78c64b4 commit 182c61b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## UNRELEASED

- Updated Allowlist display hiding Owner and Comment columns behind a expand button. This will allow for a more clean display of resources and their ID's.
- Changed the date used to calculate the age of an EC2 Instance. Instead of using the EC2 Instance `LaunchTime` which resets everytime an EC2 Instance is stopped and started, the EC2 Instance's ENI `AttachTime` is used instead. [#129](https://github.com/servian/aws-auto-cleanup/issues/129).

## 2.3.0

Expand Down
9 changes: 8 additions & 1 deletion app/src/ec2_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def instances(self):
for reservation in reservations:
for resource in reservation.get("Instances"):
resource_id = resource.get("InstanceId")
resource_date = resource.get("LaunchTime")
resource_date = self.__get_ec2_launch_time(resource)
resource_state = resource.get("State").get("Name")
resource_age = Helper.get_day_delta(resource_date).days
resource_action = None
Expand Down Expand Up @@ -672,3 +672,10 @@ def volumes(self):
else:
self.logging.info("Skipping cleanup of EC2 Volumes.")
return True

def __get_ec2_launch_time(self, resource):
for network_interface in resource.get("NetworkInterfaces"):
if network_interface.get("Attachment").get("DeviceIndex") == 0:
return network_interface.get("Attachment").get("AttachTime")

return resource.get("LaunchTime")

0 comments on commit 182c61b

Please sign in to comment.